MQL equivalent to Python's None type? - page 7

 
fxsaber:
Why do you pass Value by reference? Is it to save copy time?
 
Amir Yacoby:
Why do you pass Value by reference? Is it to save copy time?

Yes. Especially for strings.

 
fxsaber:

Yes. Especially for strings.

Not in MQL, not anymore anyways. It's actually twice as slow passing as reference in debugging, and close enough to the same when compiled normally that it really doesn't matter. Try for yourself. 

#define ITERATIONS 1000000

void OnStart()
{
   {
      ulong time = GetMicrosecondCount();
      ulong sum = 0;
      for(int i=0; i<ITERATIONS; i++){
         string r = string(rand());
         sum += by_ref(r);
      }
      time = GetMicrosecondCount() - time;
      printf("%s took %.3f milliseconds: sum=%dll", "by_ref", time/1000.0, sum);
   }{
      ulong time = GetMicrosecondCount();
      ulong sum = 0;
      for(int i=0; i<ITERATIONS; i++)
         sum += by_val(string(rand()));
      time = GetMicrosecondCount() - time;
      printf("%s took %.3f milliseconds: sum=%dll", "by_val", time/1000.0, sum);
   }
}
//+------------------------------------------------------------------+

int by_ref(string &var){ return int(var) % 100; }
int by_val(string  var){ return int(var) % 100; }

Also here is my final version of the working None-Type lib. 

#ifdef __MQL4__
#property strict
#endif 

#define NONE_DOUBLE  DBL_MAX
#define NONE_INT     INT_MAX
#define NONE_UINT    UINT_MAX
#define NONE_LONG    LONG_MAX
#define NONE_ULONG   ULONG_MAX
#define NONE_CHAR    CHAR_MAX
#define NONE_UCHAR   UCHAR_MAX
#define NONE_SHORT   SHORT_MAX
#define NONE_USHORT  USHORT_MAX
#define NONE_COLOR   clrNONE
#define NONE_DATETIME -1
#define NONE_POINTER NULL
#define NONE_STRING  "__NONE-*-*-STRING__"

class _NoneType
{
 public:
   static double   None(double _)     { return NONE_DOUBLE;   }
   static int      None(int _)        { return NONE_INT;   }
   static uint     None(uint _)       { return NONE_UINT;  }
   static long     None(long _)       { return NONE_LONG;  }
   static ulong    None(ulong _)      { return NONE_ULONG; }
   static char     None(char _)       { return NONE_CHAR;  }
   static uchar    None(uchar _)      { return NONE_UCHAR; }
   static short    None(short _)      { return NONE_SHORT; }
   static ushort   None(ushort _)     { return NONE_USHORT;}
   static string   None(string _)     { return NONE_STRING;}
   static color    None(color _)      { return NONE_COLOR;   }
   static datetime None(datetime _)   { return NONE_DATETIME;}
   template<typename T>
   static T        None(T _)          { return NONE_POINTER;}
};
#define NONE(T)  _NoneType::None((T)NULL)
 
nicholi shen:

Not in MQL, not anymore anyways. It's actually twice as slow passing as reference in debugging, and close enough to the same when compiled normally that it really doesn't matter. Try for yourself. 

Interesting, thanks!

 
nicholi shen: 

Also here is my final version of the working None-Type lib. 


You missed the float FLT_MAX

 
fxsaber:

Interesting, thanks!

This is most probably due to the optimiser, thats exactly typical optimising task. It recognizes the loop and does not copy the area 1 milion times, just one time and then use that area.

And this another recognising sign for compilers
 
It might even get wiser, seeing that parm is not changed at all and not even copy once, regardless of the loop. Thats also legit optimising. Which raises issues of testing prrformances using loops, as we do many times. Just worth mentioning.
 
Amir Yacoby:
It might even get wiser, seeing that parm is not changed at all and not even copy once, regardless of the loop. Thats also legit optimising. Which raises issues of testing prrformances using loops, as we do many times. Just worth mentioning.

Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий

Ошибки, баги, вопросы

fxsaber, 2018.10.29 07:36

#define ITERATIONS 1000000

void OnStart()
{
   string Str = "1";
   
   for (int i = 0; i < 10; i++)
     Str += Str;
   
   for (int j = 0; j < 5; j++)
   {
     {
        ulong time = GetMicrosecondCount();
        ulong sum = 0;
        for(int i=0; i<ITERATIONS; i++){
           string r = string(rand()) + Str;
           sum += by_ref(r);
        }
        time = GetMicrosecondCount() - time;
        printf("%s took %.3f milliseconds: sum=%dll", "by_ref", time/1000.0, sum);
     }{
        ulong time = GetMicrosecondCount();
        ulong sum = 0;
        for(int i=0; i<ITERATIONS; i++)
           sum += by_val(string(rand()) + Str);
        time = GetMicrosecondCount() - time;
        printf("%s took %.3f milliseconds: sum=%dll", "by_val", time/1000.0, sum);
     }
     
     Print("");
   }
}
//+------------------------------------------------------------------+

int by_ref(string &var){ return int(var) % 100; }
int by_val(string  var){ return int(var) % 100; }


by_ref took 15119.806 milliseconds: sum=-1000000ll
by_val took 13872.479 milliseconds: sum=-1000000ll

by_ref took 14433.781 milliseconds: sum=-1000000ll
by_val took 13817.533 milliseconds: sum=-1000000ll

by_ref took 13889.424 milliseconds: sum=-1000000ll
by_val took 14135.603 milliseconds: sum=-1000000ll

by_ref took 16047.643 milliseconds: sum=-1000000ll
by_val took 14494.432 milliseconds: sum=-1000000ll

by_ref took 15542.276 milliseconds: sum=-1000000ll
by_val took 13843.121 milliseconds: sum=-1000000ll
 
I am not by computer but I would try Nicholi's test with

int by_ref(const string &var){ return int(var) % 100; }
int by_val(string  var){ return int(var) % 100; 




 
Amir Yacoby:
I am not by computer but I would try Nicholi's test with

My test: the by_val works much faster than the by_ref.

Reason: