question for #define experts - page 5

 
Alexandr Andreev:

Give me the full code of your test

here you go, but I don't see the point in discussing rand() again, alternatively replace it with some inc++ variable

#define    SpeedTest(count_x10,msg,EX)        {ulong mss=GetMicrosecondCount(); ulong count=(ulong)pow(10,count_x10);for(ulong _i=0;_i<count&&!_StopFlag;_i++){EX;} \
                                              printf("%s: loops = %llu seconds=%.4f",msg,count,(double)(GetTickCount()-mss)/1000000.0);}



//+------------------------------------------------------------------+
void OnStart()
{
   int arr1[], arr2[], arr3[];
   int cnt1 = ArrayResize(arr1, 100);
   int cnt2 = ArrayResize(arr2, 200);
   int cnt3 = ArrayResize(arr3, 300);
   ulong sum = 0;

   SpeedTest(3,"ArraySize",
      for(int i = 0; i < ArraySize(arr1); i++)
      {
         for(int j = 0; j < ArraySize(arr2); j++)
         {
            for(int k = 0; k < ArraySize(arr3); k++)
            {
               arr3[k] = rand();
               sum += arr3[k];
            }
            arr2[j] = rand();
            sum += arr2[j];
         }
         arr1[i] = rand();
         sum += arr1[i];
      }
   )
   
   SpeedTest(3,"cnt",
      for(int i = 0; i < cnt1; i++)
      {
         for(int j = 0; j < cnt2; j++)
         {
            for(int k = 0; k < cnt3; k++)
            {
               arr3[k] = rand();
               sum += arr3[k];
            }
            arr2[j] = rand();
            sum += arr2[j];
         }
         arr1[i] = rand();
         sum += arr1[i];
      }
   )

}


yeah... I have an error in my code, I want to output time in seconds, but I get 10 times more, I divided it correctly by 1 000 000, who can tell me what's the reason?

 
Igor Makanu:

here you go, but I don't see the point in discussing rand() again, alternatively replace it with some inc++ variable


yeah... Some kind of error in my code, I want to output the time in seconds, but I get 10 times more, I divided it correctly by 1 000 000, who can tell me what's the reason?

BOMB.

here's your code that proves it's the other way around.

//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#define    SpeedTest(count_x10,msg,EX)        {ulong mss=GetMicrosecondCount(); ulong count=(ulong)pow(10,count_x10);for(ulong _i=0;_i<count&&!_StopFlag;_i++){EX;} \
                                              printf("%s: loops = %llu seconds=%.4f",msg,count,(double)(GetTickCount()-mss)/1000000.0);}
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void OnStart()
{
   int arr1[], arr2[], arr3[];
   int cnt1 = ArrayResize(arr1, 100);
   int cnt2 = ArrayResize(arr2, 200);
   int cnt3 = ArrayResize(arr3, 300);
   ulong sum = 0;

   
   SpeedTest(3,"cnt",
      for(int i = 0; i < cnt1; i++)
      {
         for(int j = 0; j < cnt2; j++)
         {
            for(int k = 0; k < cnt3; k++)
            {
               arr3[k] = rand();
               sum += arr3[k];
            }
            arr2[j] = rand();
            sum += arr2[j];
         }
         arr1[i] = rand();
         sum += arr1[i];
      }
   )

   SpeedTest(3,"ArraySize",
      for(int i = 0; i < ArraySize(arr1); i++)
      {
         for(int j = 0; j < ArraySize(arr2); j++)
         {
            for(int k = 0; k < ArraySize(arr3); k++)
            {
               arr3[k] = rand();
               sum += arr3[k];
            }
            arr2[j] = rand();
            sum += arr2[j];
         }
         arr1[i] = rand();
         sum += arr1[i];
      }
   )
}
 
Alexandr Andreev:

BOMB

Here's your code that proves the opposite.

I just switched the checks around.

 
Alexandr Andreev:

I just swapped the checks

2020.11.02 21:01:38.590 22222 (USDCHF,H1) cnt: loops = 1000 seconds=821.7159

2020.11.02 21:01:52.353 22222 (USDCHF,H1) ArraySize: loops = 1000 seconds=807.9415


From this it turns out thatArraySize is faster than using a variable =))) something is wrong with the test
 
Roman:

What test? ))
You yourself have shown both variations of the cycle condition.
Igor gave the code above too.
Just measure loop's performance with variable size and with ArraySize() in the loop condition.

Prove me wrong)

because in my test they are the same

 
Alexandr Andreev:

I just switched the checks around.

Yeah, you have to do that.

I was too lazy this time.

Wrapped the tests in an external loop, got it like this:

2020.11.02 22:06:43.557 SpeedTst (EURUSD,H1) ArraySize: loops = 1000 seconds=117.4626

2020.11.02 22:06:58.328 SpeedTst (EURUSD,H1) cnt: loops = 1000 seconds=102.7337

2020.11.02 22:07:13.075 SpeedTst (EURUSD,H1) ArraySize: loops = 1000 seconds=87.9782

2020.11.02 22:07:27.850 SpeedTst (EURUSD,H1) cnt: loops = 1000 seconds=73.2461

2020.11.02 22:07:42.598 SpeedTst (EURUSD,H1) ArraySize: loops = 1000 seconds=58.4859

2020.11.02 22:07:57.380 SpeedTst (EURUSD,H1) cnt: loops = 1000 seconds=43.7522

2020.11.02 22:08:12.891 SpeedTst (EURUSD,H1) ArraySize: loops = 1000 seconds=28.9861

2020.11.02 22:08:28.874 SpeedTst (EURUSD,H1) cnt: loops = 1000 seconds=13.4910

 
Igor Makanu:

Yes, it has to be done.

I was too lazy this time.

Wrapped the tests in an external loop, got this

2020.11.02 22:06:43.557 SpeedTst (EURUSD,H1) ArraySize: loops = 1000 seconds=117.4626

2020.11.02 22:06:58.328 SpeedTst (EURUSD,H1) cnt: loops = 1000 seconds=102.7337

2020.11.02 22:07:13.075 SpeedTst (EURUSD,H1) ArraySize: loops = 1000 seconds=87.9782

2020.11.02 22:07:27.850 SpeedTst (EURUSD,H1) cnt: loops = 1000 seconds=73.2461

2020.11.02 22:07:42.598 SpeedTst (EURUSD,H1) ArraySize: loops = 1000 seconds=58.4859

2020.11.02 22:07:57.380 SpeedTst (EURUSD,H1) cnt: loops = 1000 seconds=43.7522

2020.11.02 22:08:12.891 SpeedTst (EURUSD,H1) ArraySize: loops = 1000 seconds=28.9861

2020.11.02 22:08:28.874 SpeedTst (EURUSD,H1) cnt: loops = 1000 seconds=13.4910

Now swap them in the loop and be amazed

 
Alexandr Andreev:

Now swap them in the loop and you will be surprised

I will not be surprised. I know that the compiler can optimize code on the fly

but, imho, in nested loops, you still should not call ArraySize() unnecessarily.

for(int i = ArraySize(arr)-1; i >=0 ; i--)

Of course, it may sometimes be inconvenient, so I do the loop via a temporary variable - your version № 2

imho, it is reliable and you understand what will happen

 

Well I can't say anything against IMHO.

On very large replays, my wins have become random for the first and second method... Most likely it became dependent on current CPU cache and overall load.

My question wasn't about the loop - it was about how the function unfolded. Just as an example was ArraySize

The result;

for (int i=0; i<ArraySIze(mas); i++) == for (int i=0; i<size; i++)


But if we add the initialization of the size variable to the second part, then the first method takes the lead for the time of initializing this variable and equating it with the value.

 

If you change the size of arrays in the body of the loop, on-the-fly optimization does not work

so change the code:

for(int loop=0; loop <5;loop++)
   {
   int cnt1 = ArrayResize(arr1, 100+loop);
   int cnt2 = ArrayResize(arr2, 200+loop);
   int cnt3 = ArrayResize(arr3, 300+loop);
   
   SpeedTest(3,"cnt",
.....

Alexandr Andreev:

then the first method takes precedence, for the time of initializing this variable and equating it with a value, it's noticeable only at high repetitions

it doesn't work

runtime optimization will be in the lead

you can't test such simple mashidas without the profiler, in general, you can write in a loop, or you can test in the tester, speed is important for it