question for #define experts - page 8

 
void OnStart()
  {
   int mas[];
   int mas1[300];
   int mas2[300];
   int mas3[300];
   int mas4[300];
   int mas5[300];
   int mas6[300];
   int z=300;
   int size=100000000;
   ArrayResize(mas,size);
   ulong r=0;
   ulong r1=0;
   ulong r2=0;
   int random;
   ulong max=100; 
   int t=0;
   int tr=0; 
   MathSrand(10);
    int num_steps=ArraySize(mas);
    double x, pi, sum=0.0;
    double step = 1.0/(double)num_steps;
    
     int v=size;
    ulong t1 = GetMicrosecondCount();
     
    
  // for(ulong z=0; z<max; z++)
     {
      for(ulong i=0; i<ArraySize(mas); i++)
        { 
        r2+=ArraySize(mas);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2=r2/10;
        }

     }  
   ulong t2=GetMicrosecondCount();
   //for(ulong z=0; z<max; z++)
     {
      for(ulong i=0; i<v; i++)
        { 
        r1+=v;
        r1=r1/10;
        }
     }
   
   int pi2 = sum*step;
   ulong t3=GetMicrosecondCount();
   Print(t2-t1,"  ",t3-t2," ",r," ",r1," ",r2," ",pi," ",pi2);
// Templ();
  }

Even so, the top code sometimes wins, but very rarely, i.e. the link is free

 
Alexandr Andreev:

) well that's not how it works)


That's how it works in C++
I think it's the same in mql but with additional wrappers from MQ


Forum on trading, automated trading systems & strategy testing

FAQ from Beginners MQL5 MT5 MetaTrader 5

Roman, 2019.12.11 14:02

You don't have to think it through, why should I... The compiler will do it all by itself. ))
C# is not C

Take a look at the video about __inline.
It explains there how functions work in memory for those who don't make any difference.


 
Roman:

This is how it works in C++
In mql I think the same, but with additional wrappers from MQ

Well, now reread this thread and a lot of statements and test examples - that there is some difference. And we found it)))

 
Alexandr Andreev:

Well now reread this thread and a bunch of statements and examples of tests - that there is a difference. And they did))))

No )) I have no desire to reread it.
It's obvious to me that there is a difference.

 
Roman:

No )) I have no desire to reread it.
It's obvious to me that there is a difference.

)))) another IMHO.

 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   int mas[];
   int mas1[300];
   int mas2[300];
   int mas3[300];
   int mas4[300];
   int mas5[300];
   int mas6[300];
   int z=300;
   int size=1000000000;
   ArrayResize(mas,size);
   ulong r=0;
   ulong r1=0;
   ulong r2=0;
   int random;
   ulong max=100; 
   int t=0;
   int tr=0; 
   MathSrand(10);
    int num_steps=ArraySize(mas);
    double x, pi, sum=0.0;
    double step = 1.0/(double)num_steps;
    
     int v=size;
    ulong t1 = GetMicrosecondCount();
     
    
  // for(ulong z=0; z<max; z++)
     {
      for(int i=0; i<ArraySize(mas); i++)
        { 
        r2+=ArraySize(mas);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1);
        r2+=ArraySize(mas2);
        r2+=ArraySize(mas3);
        r2+=ArraySize(mas4);
        r2+=ArraySize(mas5);
        r2+=ArraySize(mas6);
        r2+=ArraySize(mas1); 
        r2++;
        r2=r2/100;
        }

     }  
   ulong t2=GetMicrosecondCount();
   //for(ulong z=0; z<max; z++)
   int sizem=ArraySize(mas);
     {
      for(int i=0; i<sizem; i++)
        { 
        r1++;
        r1=r1/100;
        r1++;
        
        }
     }
    
   ulong t3=GetMicrosecondCount();
   Print(t2-t1,"  ",t3-t2," ",r2," ",r1);
// Templ();
  }

//+------------------------------------------------------------------+
//|                                           
  
void Funk(ulong &a){a++;}
void FunkRand(ulong &a){a+=rand();}

//+------------------------------------------------------------------+

The upper way is faster by almost 15% this is very significant, well if everything is so obvious explain it to me)


 
Alexandr Andreev:

)))) another IMHO

Top way is faster by almost 20%, well since everything is so obvious explain it to me)


The loops being compared are not the same in terms of code in the body.
The first loop has one code in the body, the second loop's body has another code.
Naturally different code instructions, naturally different execution time.
Make the same code in the loop body and change only the loop condition ArraySize and the size variable.
We are testing this part, not the body.

 
Roman:

The loops being compared are not the same code in the body.
The first loop has one code in its body and the second loop's body has another code.
Naturally, different code instructions and execution time are different.
Make the same code in the loop body and change only the loop condition ArraySize and the size variable.
We are testing this part, not the body.

Your test is more incorrect because it depends on the launch case, run it again. Here in both cases there is an increment and one division. Well, plus there are a couple of extra tens~ billions of ArraySize calls on top.

By the way, it's in the body that we should write what we're testing. Because it's the body that gets repeated. We are trying to wrap it into loop.... to get the result i.e. it was originally necessary tocall ArraySize from the body

Документация по MQL5: Операции с массивами / ArraySize
Документация по MQL5: Операции с массивами / ArraySize
  • www.mql5.com
"Нулевое измерение = Размер массива / (Первое измерение * Второе измерение * Третье измерение)"
 
Alexandr Andreev:

Your test is more incorrect as it depends on the launch case, run it again. Here both cases have increment and one division. Well, plus there are a couple of tens~ billions of ArraySize calls on top of it.

By the way, it's in the body that we should write what we're testing. Because it's the body that gets repeated. We are trying to wrap it into loop.... to get a result i.e. it was initially necessary tocall ArraySize from the body

At each iteration, the loop's condition already contains a check of the i<ArraySize() or i<size
condition, i.e. at each iteration either a function or a variable is called.
Why should we put the object being tested into the body?

Logic itself prompts us to decide which one will be faster to handle. To a function or to a variable.
I don't care what the compiler calls it. I'm not relying on the compiler, I'm just using my common sense to figure out what's faster to handle from the reference point of view.

 
Roman:

At each iteration, in the loop's condition, the condition i<ArraySize() or i<size
is checked anyway, which means that at each iteration either a function or a variable is accessed.
Why should we put the object being tested in the body?

Because we are the lucky ones who have this function and the function can be any other. And it is placed exactly in the body. I only duplicated it in an attempt to enhance its effect and address different arrays.

But it is wrong to add more complex tasks, the error of calculation of which may overshadow the effect under study. By the way it is also possible that the assembly is not constant in μl. i.e. when recompiling can get slightly different data (although this is not exact, but it's kind of used as protection against hacking) So everything can be tested on your code for you. See if the results change.

Mcl simply tries to substitute code as indicated in the video. Well it's a little bit different there. But in general terms.

And those instructions for functions that don't know what value they will get - that's how js and php and other similar languages work, even µl works this way but only in debug mode

Документация по MQL5: Константы, перечисления и структуры / Состояние окружения / Информация о запущенной MQL5-программе
Документация по MQL5: Константы, перечисления и структуры / Состояние окружения / Информация о запущенной MQL5-программе
  • www.mql5.com
Информация о запущенной MQL5-программе - Состояние окружения - Константы, перечисления и структуры - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Roman:

At each iteration, in the loop's condition, the condition i<ArraySize() or i<size
is checked anyway, which means that at each iteration either a function or a variable is accessed.
Why should we put the object being tested into the body?

Logic itself prompts us to decide which one will be faster to handle. To a function or to a variable.
I don't care how the compiler binds it there. I don't rely on the compiler, I rely on common sense and figure out what is faster to handle from the reference point of view.

It doesn't always work.

Forum on trading, automated trading systems and trading strategy testing

Question for #define experts

Roman, 2020.11.02 19:44

Changed my post.
It is the other way round, i.e. ArraySize is faster now than cnt.
It was vice versa before. Maybe increment cnt-- affects, loop's body is different and probably something else must be invented for the load.

void OnStart()
{
   int arr[];
   int sz = ArrayResize(arr, 100000000);  
   
   ulong t = GetMicrosecondCount();
   
   for(int i=0; i < ArraySize(arr); i++) 
   {
      ArrayResize(arr, sz--); //какая то нагрузка
   }   
   
   t = GetMicrosecondCount() - t;
   
   PrintFormat("Total time: %.3f ms", t / 1000.0);
}
2020.11.02 21:33:22.863 TestScript (USDJPY,M1)  Total time: 451.200 ms


void OnStart()
{
   int arr[];
   int sz = ArrayResize(arr, 100000000);  
   int cnt = ArraySize(arr);
   
   ulong t = GetMicrosecondCount();
   
   for(int i=0; i < cnt; i++) 
   {
      ArrayResize(arr, sz--);
      cnt--;
   }
      
   t = GetMicrosecondCount() - t;
   
   PrintFormat("Total time: %.3f ms", t / 1000.0);
}
2020.11.02 21:56:26.591 TestScript (USDJPY,M1)  Total time: 531.872 ms

Reason: