Errors, bugs, questions - page 2880

 
Igor Makanu:

no

if there are repetitive code sections, you will get optimization testing!

What difference does it make how long rand() is executed?

Let it be executed 2/3 of the test time, the main thing is to make rand() time constant

Your MQL code will use system MQL-functions, right? - what's the point of testing a perfect code?

)) the difference in speed could be 4 times in favor of one of the methods (1 to 4), but since the rand operation is 10 times slower than the rest of the code, this difference would not be visible (11 to 14)

 
Alexandr Andreev:

)) The difference in speed could have been 4 times in favour of one of the methods (1 to 4), but as the operation rand 10 times longer than the rest of the code, this difference would not have been visible (11 to 14)

no it couldn't

the difference is immediately visible

let's check something developers announced in the innovations, they often write that timestamp access time for some function was optimized = not checked for a long time

#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;} \
//+------------------------------------------------------------------+
void OnStart()
{
   double sum1 = 0.0;
   SpeedTest(7, "tst 1 : ",
   {
      sum1 += iOpen(NULL, 0, rand());
   });
//--
   double sum2 = 0.0;
   SpeedTest(7, "tst 2 : ",
   {
      double o[];
      CopyOpen(NULL, 0, rand(), 1, o);
      sum2 += o[0];
   });
   Print(sum1, " : ", sum2);
}
//+------------------------------------------------------------------+

2020.10.16 02:11:20.671 SpeedTst (EURUSD,H1) tst 1 : : loops=10000000 ms=353174

2020.10.16 02:11:21.967 SpeedTst (EURUSD,H1) tst 2 : : loops=10000000 ms=1296043

2020.10.16 02:11:21.967 SpeedTst (EURUSD,H1) 11304533.15558525 : 11303930.69247558

2020.10.16 02:11:44.012 SpeedTst (EURUSD,H1) tst 1 : : loops=10000000 ms=359757

2020.10.16 02:11:45.369 SpeedTst (EURUSD,H1) tst 2 : : loops=10000000 ms=1357325

2020.10.16 02:11:45.369 SpeedTst (EURUSD,H1) 11304350.05612442 : 11304321.21848488

 
Igor Makanu:

not critical at all

rand() execution time is constant, most likely it is a common C++ function, google "rand c++ source code"

you must initialize it, but if you initialize it with constants you may encounter optimization

in general, i don't understand the dislike of rand()

an option is to initialize with something like this:

would be fast.


ran your script, imho not correct.

The main time is loading code into the cache and then into the processor

and add the discreteness of the system timer

we get... almost a random number.

you need to test it for a long time, about 100500 times, imho

rand() silences the result.
On my computer, one loop iteration takes about 1.5 nanoseconds.

With two rand(), one iteration takes twice as long - up to nearly 3 nanoseconds. You should realize that in this iteration two accesses to an item of a large array will vanish, which is also rather expensive.
It means that about 2/3 of all the iteration time is spent on two rands(). Of course, I got a little excited about "by an order of magnitude" ))

I don't understand how one can run into optimization when a pre-prepared array is filled with random numbers.

It's already discussed rand() performance once. I even tried to write a similar function myself.https://www.mql5.com/ru/forum/170952/page137#comment_12010041

#define  Num 10000000
#define  SpeedTest(msg,s,EX)  {ulong mss=GetMicrosecondCount(); EX \
                                    mss=GetMicrosecondCount()-mss;\
                                    printf("%-30s%llu µs; Сумма - %llu",msg,mss,s);}
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   ulong sum = 0;
   ushort in[];
   ArrayResize(in,Num*2);
   for (int i=0; i<Num*2; i++) in[i] = (ushort)rand();
   SpeedTest("test binary shift : ", sum,
   for (int i =0; i<(2*Num-1);i+=2)
   {
      sum+=in[i]<<(sizeof(ushort)*8) | in[i+1];
   });
//+------------------------------------------------------------------+ 
   sum = 0;
   union ushortTouint
   {
      uint param;
      ushort in[2];
   } u;
   SpeedTest("test union : ", sum,
   for (int i =0; i<(2*Num-1);i+=2)
   {
      u.in[0]=in[i+1];
      u.in[1]=in[i];
      sum+=u.param;
   });
//+------------------------------------------------------------------+   
   sum=0;
   SpeedTest("test binary shift + rand() : ", sum,
   for (int i =0; i<(2*Num-1);i+=2)
   {
      sum+=(ushort)rand()<<(sizeof(ushort)*8) | (ushort)rand();
   });
//+------------------------------------------------------------------+  
   sum = 0;
   SpeedTest("test union + rand() : ", sum,
   for (int i =0; i<(2*Num-1);i+=2)
   {
      u.in[0]=(ushort)rand();
      u.in[1]=(ushort)rand();
      sum+=u.param;
   });
}

result

2020.10.15 18:30:34.752 TestMakanu (USDCAD,M1)  test binary shift :           13474 µs; Сумма - 10736778112806491
2020.10.15 18:30:34.763 TestMakanu (USDCAD,M1)  test union :                  10189 µs; Сумма - 10736778112806491
2020.10.15 18:30:34.792 TestMakanu (USDCAD,M1)  test binary shift + rand() :  29103 µs; Сумма - 10741215942967312
2020.10.15 18:30:34.820 TestMakanu (USDCAD,M1)  test union + rand() :         28125 µs; Сумма - 10737655794873175
2020.10.15 18:30:37.802 TestMakanu (USDCAD,M1)  test binary shift :           11144 µs; Сумма - 10739125609966082
2020.10.15 18:30:37.812 TestMakanu (USDCAD,M1)  test union :                  10334 µs; Сумма - 10739125609966082
2020.10.15 18:30:37.840 TestMakanu (USDCAD,M1)  test binary shift + rand() :  27495 µs; Сумма - 10739749025492645
2020.10.15 18:30:37.867 TestMakanu (USDCAD,M1)  test union + rand() :         26782 µs; Сумма - 10738717766184542
2020.10.15 18:30:40.356 TestMakanu (USDCAD,M1)  test binary shift :           10618 µs; Сумма - 10738510882058397
2020.10.15 18:30:40.367 TestMakanu (USDCAD,M1)  test union :                  11354 µs; Сумма - 10738510882058397
2020.10.15 18:30:40.395 TestMakanu (USDCAD,M1)  test binary shift + rand() :  27637 µs; Сумма - 10738670876702722
2020.10.15 18:30:40.422 TestMakanu (USDCAD,M1)  test union + rand() :         26772 µs; Сумма - 10737441784096963
 

I think the bottom line is that it makes almost no difference whether you use union or binary shift.
I use both of these methods in my iCanvas library to work with colour.

union argb {
   uint clr;
   uchar c[4];
};


I remember, I've been doing some performance testing too, and I've concluded that it makes almost no difference. But the code with union is more readable.
I must admit that I was disappointed because I was expecting to get a noticeable performance gain with union.

 
Nikolai Semko:

I don't understand how you can run into optimization when a pre-prepared array is filled with random numbers.

I gave you 2 links on the hubr above, the first article shows well how a normal loop looks like after compilation.

I'm not saying that you can necessarily get to runtime optimization, but imho, if the array does not change, which is essentially a constant memory space with a constant size for the loop, it is not a fact that optimization will not pop up on some type of processor, and the optimization can be expected from the compiler, as well as from the processor with a cache


Nikolai Semko:

I think the bottom line is that it makes almost no difference what to use - union or binary shift.

Yes, that's right, I will use shift, the code is readable, and there is no need to introduce a new data type - union
 
Igor Makanu:

I gave you 2 links on the hbr above, the first article shows well how a normal loop looks after compilation

I'm not saying that you must get to runtime optimization, but imho, if the array does not change, which is essentially a constant memory space with a constant size for the loop, it's not certain that no optimization will pop up

Judging by the results, there is no sign of optimization

 
Nikolai Semko:

The results don't give the impression of optimisation at all

yes

The argument is not about a specific code

but in the methodology of testing, I still stick to what I test optimally, and rand() although it introduces distortions, but it is a linear coefficient, not proportional, i.e. the accuracy is not very important if there is no difference in speed of less than 5%, imho

 
Dear people, can anyone at least give me an answer to this situation that I do not understand?
 

Hello, everyone!

I have a question, maybe to MQL5 developers? Maybe someone else knows... Are there plans to integrate ME compiler/code editor with some popular development environments like IDEA or Visual Studio? Meta Editor is a big pain for me. No block collapsing, no auto-substitution templates (e.g. when you start typing for(...) it immediately offers you to substitute a loop pattern and many other things), no highlighting of various important parts of the code. And so on and the like. I'm not saying that modern robots are complicated, these are large projects and there are much more serious demands to code management and team development capabilities.

No one writes their own editors and compilers any more - they all give them up and move to ready-made solutions from Microsoft, JetBrains and others. All modern editors have systems of customizable plugins that allow you to add anything you like to them. The task is not difficult in principle.

 
Сергей Таболин:
Dear Sirs, is there anyone who can give me an answer to this situation that I do not understand?

And you get even more kicking out of the programmers. Then you'll get help for sure.