Errors, bugs, questions - page 2878

 
Igor Makanu:

which conversion variant will work faster during optimisation

This will only be called once per pass. Therefore, it makes no difference.

 
fxsaber:

This will only be called once for the entire passage. So it makes no difference.

so yeah... But I'm going to run it for a couple of days for optimization, I want to measure the performance in some way ..... although I suspect variant 1 will be more efficient, there won't be 100500 times of copying into arrays

 
Igor Makanu:

yes... But I'm going to run the computer for a couple of days to optimise it, I want to measure the performance in some way ..... although I suspect option 1 will be more efficient, there won't be 100500 times of copying to arrays

So variant 2 is most likely faster, but as you pointed out above, it doesn't matter.
 
TheXpert:
union is not copying to arrays, it's different interpretation of the same memory space. so the 2nd option is probably faster, but as noted above, it doesn't matter.
Union is not as fast as it seems. Unfortunately.
I'm betting on the first one.
 
Igor Makanu:

yes... But I'm going to run the computer for a couple of days to optimise it, I want to measure the performance in some way ..... although I suspect option 1 will be more efficient, there won't be 100500 times of copying to arrays

there's an old way to measure speed

in the style of

for (int i=0; i< 1000000; i++) {our code1}....
for (int i=0; i< 1000000; i++) {our code2}....

 
Nikolai Semko:
Union is not as fast as it sounds. I bet on the first one.

I bet on the first one too! Binary operators are several times faster than the if operator, although the second one does not have it.

 

checked:

#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 ms=%llu",msg,count,GetMicrosecondCount()-mss);}
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   ulong sum = 0;
   SpeedTest(10, "tst 1 : ",
   {
      ushort in01A = (ushort)rand();
      ushort in01B = (ushort)rand();
      ushort in02A = (ushort)rand();
      ushort in02B = (ushort)rand();
      ushort in03A = (ushort)rand();
      ushort in03B = (ushort)rand();
      ushort in04A = (ushort)rand();
      ushort in04B = (ushort)rand();
      ushort in05A = (ushort)rand();
      ushort in05B = (ushort)rand();
      uint param[5];
      param[0] = (uint)in01A << (sizeof(ushort) * 8) | in01B;
      param[1] = (uint)in02A << (sizeof(ushort) * 8) | in02B;
      param[2] = (uint)in03A << (sizeof(ushort) * 8) | in03B;
      param[3] = (uint)in04A << (sizeof(ushort) * 8) | in04B;
      param[4] = (uint)in05A << (sizeof(ushort) * 8) | in05B;
      for(int i = 0; i < 5; i++) sum += param[i];
   });
//--   
   sum = 0;
   union ushortTouint
   {
      uint param[5];
      ushort in[10];
   }U;
   SpeedTest(10, "tst 2 : ",
   {
      ushort in00 = (ushort)rand();
      ushort in01 = (ushort)rand();
      ushort in02 = (ushort)rand();
      ushort in03 = (ushort)rand();
      ushort in04 = (ushort)rand();
      ushort in05 = (ushort)rand();
      ushort in06 = (ushort)rand();
      ushort in07 = (ushort)rand();
      ushort in08 = (ushort)rand();
      ushort in09 = (ushort)rand();
      ushortTouint u;
      u.in[0] = in00;
      u.in[1] = in01;
      u.in[2] = in02;
      u.in[3] = in03;
      u.in[4] = in04;
      u.in[5] = in05;
      u.in[6] = in06;
      u.in[7] = in07;
      u.in[8] = in08;
      u.in[9] = in09;
      for(int i = 0; i < 5; i++) sum += u.param[i];
   });

}

2020.10.15 21:48:01.401 SpeedTst (EURUSD,H1) tst 1 : : loops=10000000000 ms=10864370

2020.10.15 21:48:12.264 SpeedTst (EURUSD,H1) tst 2 : : loops=10000000000 ms=10862287

the difference is not significant, it is highly probable that if we switch the tests in the opposite order, the results will be the opposite

not critical

 
Igor Makanu:

not a significant difference, it is highly probable that if you swap the tests, the results will be the opposite

It is highly probable that the compiler has generated the same code for both cases. In this case, just choose the one you subjectively like better

 
Igor Makanu:

checked:

2020.10.15 21:48:01.401 SpeedTst (EURUSD,H1) tst 1 : : loops=10000000000 ms=10864370

2020.10.15 21:48:12.264 SpeedTst (EURUSD,H1) tst 2 : : loops=10000000000 ms=10862287

the difference is not significant, it is highly probable that if we switch the tests in the opposite order, the results will be the opposite

not critical

The script here demonstrates that the time of random number creation may be non-uniform and depends on the volume of created variables)))

And the code we need in this number of repetitions takes me 0 ms.

Still no answer.

or the code optimiser is cutting out something unnecessary
 
Alexandr Andreev:

There seems to be a script that demonstrates that random number generation times can be unequal

no rand() is a normal function, it always works the same

But when testing speed, if you initialize it with constants, the tests will "speed up" during execution - code optimization in MQL during runtime is good

in general, it was checked many times

Alexandr Andreev:

and a lot depends on the size of created variables)))

of course, allocating memory is time consuming, I checked it, I'm testing both dynamically created objects ( new pointer ) and just objects in local scope, the test stretches 100500 times per new + delete

the whole question was, because in the tester in global scope memory is allocated to variables once and not every pass - but I need arrays uint, so I tested with this script, not as I wrote it the first time

Alexandr Andreev:

And the code we need in this number of repetitions takes me 0 ms

still no answer

Either the code optimiser is cutting something unnecessary.

did you use my script? - either you didn't wait till the end of the test and interrupted or you overflowed ulong - the first macro parameter is 10^ count



Andrei Trukhanovich:

it is highly probable that the compiler has generated the same code for both cases. in this case, just choose the one you subjectively like best.

yes, maybe so

that's what I was asking - I've read many times that modern processors can execute more than one elementary operation per clock by optimizing the instruction pipeline .... a lot of blah-blah-blah... and the point is that the arithmetic instructions are executed by the processor in an unpredictable number of clock cycles

As for branching and memory allocation operations, they are very badly optimized by the processor, so don't look for optimization in simplifying arithmetic, try to write maximally linear code with minimum branching, and variables are better declared and assigned values right before calculations, that allows instruction pipeline and cache sampling prediction to optimize this code


I.e. sampling values of elements in the array (addressing) most likely won't be crucial for speed - I thought there would be a shift advantage vs. union, it turns out there is no difference at all

Reason: