I need few people to benchmark this MQL4 Script

 

Hey there. I'm doing some benchmarks and I wonder what will be the result for other people. Can you try the script below and tell me the time returned for f1(), f2() and f3()?

 

void OnInit()
{
   int i, t0, count = 10000000;
   
   //--
   t0 = GetTickCount();
   for (i=0; i<count; i++)
   {
      f1();
   }
   Print("f1() : "+(GetTickCount()-t0)+"ms");
   
   //--
   t0 = GetTickCount();
   for (i=0; i<count; i++)
   {
      f2();
   }
   Print("f2() : "+(GetTickCount()-t0)+"ms");
   
   //--
   t0 = GetTickCount();
   for (i=0; i<count; i++)
   {
      f3();
   }
   Print("f3() : "+(GetTickCount()-t0)+"ms");
}

void f1()
{
   return;

   while(true)
   {
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
      Comment(OrderComment());
   }
}

void f2()
{
   return;

   while(true)
   {   
      Comment(OrderComment());
   }
}

void f3()
{
   return;

   while(true)
   {
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
      Comment(GlobalVariableGet("i"));
   }
}
 
JDeel: I'm doing some benchmarks and I wonder what will be the result for other people. Can you try the script below and tell me the time returned for f1(), f2() and f3()?
Start debugging (next to Compile)Drag script from Navigator
f3() :  125ms  110ms  109ms  125ms
f2() :  234ms  250ms  282ms  250ms
f1() : 7891ms 7672ms 7640ms 8032ms
f3() :  157ms  141ms  125ms  125ms
f2() :  234ms  250ms  234ms  297ms
f1() : 7766ms 8000ms 7906ms 7891ms

That is unexpected. Looked to me like JIT compiling so I:

modified your code No difference
void OnInit()
{f1(); f2(); f3();
Intel(R) Core(TM) i5-3337U CPU @ 1.80GHz, 1801 Mhz,
f3() :  109ms  156ms  156ms  109ms
f2() :  250ms  234ms  265ms  235ms
f1() : 7875ms 7797ms 8000ms 8265ms
 
JDeel:

Hey there. I'm doing some benchmarks and I wonder what will be the result for other people. Can you try the script below and tell me the time returned for f1(), f2() and f3()?

I get a similar pattern of results to WHRoeder.

It looks like pre-allocation of string handling. For example, I get much the same results for f1() if I replace all the uses of OrderComment() with a call to the following dummy function TestStringReturn():

string TestStringReturn() {return "";}

Hypothetically, f3() is faster because GlobalVariableGet() returns a double, rather than requiring string allocation.

And it's not an overhead related to compilation of the f1() function itself. The time taken for the f1() loop seems basically to be linear depending on the number of iterations; count = 5000000 takes about half as long.

 

This is unpredictable and surely happens to mid-size and complex EAs. Put some functions in a loop (it can be "for" loop as well) and then you will need to wait times longer for the backtest to over only because of the content of the code, not because of what was actually executed.

In fact, the count of functions in the loop matters. If I have 47 rows of:

Comment(OrderComment());

 ... there is no problem at all. If I have 48 rows, then the problem is here.

47 is my critical number, it might be different on other machines, I'm not sure. Well, because I have 2 functions in a row, the actual critical number of functions is double. And I can replace some functions with declarations of variables, the result will be the same.

WHRoeder, I have slower CPU and my fast times are faster than yours, only 47ms :P Well, f3() is almost 12 seconds, this was expected :)

jjc, yes, there is something with strings. Instead of using functions, you can define few strings like in the code below, and still get this issue. In this case my critical number of strings is 18.

void test()
{
   return;

   while(true)
   {
      string a1 = "";
      string a2 = "";
      string a3 = "";
      string a4 = "";
      string a5 = "";
      string a6 = "";
      string a7 = "";
      string a8 = "";
      string a9 = "";
      string a10 = "";
      string a11 = "";
      string a12 = "";
      string a13 = "";
      string a14 = "";
      string a15 = "";
      string a16 = "";
      string a17 = "";
      string a18 = "";
      //string a19 = ""; // uncomment to make it run slow
   }
}
 
jjc:

It looks like pre-allocation of string handling.

... And I get the same broad set of results for a version of f1() which uses string manipulation but which doesn't use either Comment() or OrderComment(). For example:

void f1()
{
   return;

   while(true)
   {
      TestOutput(TestStringReturn());
      TestOutput(TestStringReturn());
      TestOutput(TestStringReturn());

      [ ... ETC ... ]
   }
}

void TestOutput(string x)
{
   string v = "";
   StringToLower(v);
}

string TestStringReturn() {return "";}
Reason: