Possible conditional check error - page 3

 
Dominik Egert:
A lot of APIs in the financial sector are open, just think of all the crypto exchanges...

It's good and normal practice to have a secure API. And security on API level as well.

To me, I cannot see the benefit of it being closed source. In contrast, I think an OS Version would benefit all parties.

But that's also only my thinking.

A Linux version would be nice as well as a native Mac Version.

Or think of all the great mobile app adoptions that would happen...

Thanks for not answering to my question 

 
To answer your Questions properly, I will be required to take the time and sit at my PC.

Currently I am on mobile
 
Dominik Egert:
To answer your Questions properly, I will be required to take the time and sit at my PC.

Currently I am on mobile

It happens :-)

 
Alain Verleyen:

I don't see the difference either. The compiler should optimize the expression.

I don't get your point about "10 times faster". What exactly is 10 times faster than what...exactly ?

 
Since I am still only on mobile and I want to get back to your question.

Unconditional vs conditional code.

I will post an example later, but for now try a Google or YouTube search on the matter.


 
Dominik Egert:
Since I am still only on mobile and I want to get back to your question.

Unconditional vs conditional code.

I will post an example later, but for now try a Google or YouTube search on the matter.


I don't want to search. I want your detailed answer. loool

 
Alain Verleyen:

I don't want to search. I want your detailed answer. loool

OK, here is a very simple example. I hope this is enough research on my side to cover your need of details on unconditional code.


// Create random data

uint    left_ptr    = NULL;
uint    right_ptr   = NULL;
uchar   eval        = NULL;

uchar   data[0xFFFFFF];
uchar   left[0xFFFFFF];
uchar   right[0xFFFFFF];

ulong   start       = NULL;
ulong   stop        = NULL;
uint    loop_cnt    = NULL;

for(uint cnt = NULL; cnt < 0xFFFFFF; cnt++) 
{ data[cnt] = (uchar)MathRand(); }



left_ptr = NULL;
right_ptr = NULL;
start = GetMicrosecondCount();
for(uint cnt = NULL; cnt < 0xFFFFFF; cnt++)
{
    if(data[cnt] > 0x7F)
    { 
        left[left_ptr] = data[cnt]; 
        left_ptr++;
    }

    if(data[cnt] < 0x80)
    { 
        right[right_ptr] = data[cnt]; 
        right_ptr++;
    }
    
    loop_cnt++;
}
stop = GetMicrosecondCount();
printf("IF IF Loop time: %llu nanosec; total time: %llu microsec", ((stop - start) * 1000) / loop_cnt, stop - start);



left_ptr = NULL;
right_ptr = NULL;
start = GetMicrosecondCount();
for(uint cnt = NULL; cnt < 0xFFFFFF; cnt++)
{
    if(data[cnt] > 0x7F)
    { 
        left[left_ptr] = data[cnt]; 
        left_ptr++;
    }

    else if(data[cnt] < 0x80)
    { 
        right[right_ptr] = data[cnt]; 
        right_ptr++;
    }
    
    loop_cnt++;
}
stop = GetMicrosecondCount();
printf("IF ELSE IF Loop time: %llu nanosec; total time: %llu microsec", ((stop - start) * 1000) / loop_cnt, stop - start);


left_ptr = NULL;
right_ptr = NULL;
start = GetMicrosecondCount();
for(uint cnt = NULL; cnt < 0xFFFFFF; cnt++)
{
    if(data[cnt] > 0x7F)
    { 
        left[left_ptr] = data[cnt]; 
        left_ptr++;
    }

    else
    { 
        right[right_ptr] = data[cnt]; 
        right_ptr++;
    }
    
    loop_cnt++;
}
stop = GetMicrosecondCount();
printf("IF ELSE Loop time: %llu nanosec; total time: %llu microsec", ((stop - start) * 1000) / loop_cnt, stop - start);




left_ptr = NULL;
right_ptr = NULL;
start = GetMicrosecondCount();
for(uint cnt = NULL; cnt < 0xFFFFFF; cnt++)
{
    eval = (data[cnt] > 0x7F);
    left[left_ptr] = (data[cnt] * eval) + (left[left_ptr] * !(eval)); 
    left_ptr += eval;

    right[right_ptr] = (data[cnt] * !(eval)) + (right[right_ptr] * eval); 
    right_ptr += eval;
    
    loop_cnt++;
}
stop = GetMicrosecondCount();
printf("Unconcditional Loop time: %llu nanosec; total time: %llu microsec", ((stop - start) * 1000) / loop_cnt, stop - start);




These are my results on my tablet...

IF IF Loop time: 5 nanosec; total time: 84024 microsec

IF ELSE IF Loop time: 2 nanosec; total time: 89009 microsec

IF ELSE Loop time: 1 nanosec; total time: 84006 microsec

Unconcditional Loop time: 0 nanosec; total time: 39527 microsec


Noticable is the time difference given for each loop run, as well as the distribution of execution total compared to single runs. Although this is not a precise measurement, by the nature of statistics, this should be correct.

 
Dominik Egert:

OK, here is a very simple example. I hope this is enough research on my side to cover your need of details on unconditional code.



These are my results on my tablet...

IF IF Loop time: 5 nanosec; total time: 84024 microsec

IF ELSE IF Loop time: 2 nanosec; total time: 89009 microsec

IF ELSE Loop time: 1 nanosec; total time: 84006 microsec

Unconcditional Loop time: 0 nanosec; total time: 39527 microsec


Noticable is the time difference given for each loop run, as well as the distribution of execution total compared to single runs. Although this is not a precise measurement, by the nature of statistics, this should be correct.

Thank you.

Ok so as I suspected it's far from 10 times. More around 2 times ;-)

There are two small bugs in your code :

1. your forgot to reset loop_cnt (unless you did it on purpose ? but why).

2. It should be     

right_ptr += !eval;

And here is a more then 20 times faster solution for this specific example.

2021.07.10 15:30:34.586    372818 (EURUSD,M1)    Unconcditional Loop time: 1.9 nanosec; total time: 31187 microsec. Left = 1606418432 Right = 532676575
2021.07.10 15:30:34.659    372818 (EURUSD,M1)    IF IF Loop time: 3.9 nanosec; total time: 66114 microsec. Left = 1606418432 Right = 532676575
2021.07.10 15:30:34.734    372818 (EURUSD,M1)    IF ELSE IF Loop time: 4.1 nanosec; total time: 68087 microsec. Left = 1606418432 Right = 532676575
2021.07.10 15:30:34.809    372818 (EURUSD,M1)    IF ELSE Loop time: 4.0 nanosec; total time: 67479 microsec. Left = 1606418432 Right = 532676575
2021.07.10 15:30:34.830    372818 (EURUSD,M1)    TERNARY ? Loop time: 0.2 nanosec; total time: 3015 microsec. Left = 1606418432 Right = 532676575

I fixed the bugs and added quick checking of the solutions.

Thanks for your time it was funny.

Files:
372818.mq5  6 kb
 

Ohh, I am sorry... - I messed up the measurement in the code. I have attached a file with some other measurements and corrected loop_cnt value.

Files:
Testscript.mq5  18 kb
 
Alain Verleyen:
Thank you.

Ok so as I suspected it's far from 10 times. More around 2 times ;-)

There are twosmall bugs in your code :

1. your forgot to reset loop_cnt (unless you did it on purpose ? but why).

2. It should be     

right_ptr += !eval;

And here is a more then 20 times faster solution for this specific example.

2021.07.10 15:30:34.586    372818 (EURUSD,M1)    Unconcditional Loop time: 1.9 nanosec; total time: 31187 microsec. Left = 1606418432 Right = 532676575
2021.07.10 15:30:34.659    372818 (EURUSD,M1)    IF IF Loop time: 3.9 nanosec; total time: 66114 microsec. Left = 1606418432 Right = 532676575
2021.07.10 15:30:34.734    372818 (EURUSD,M1)    IF ELSE IF Loop time: 4.1 nanosec; total time: 68087 microsec. Left = 1606418432 Right = 532676575
2021.07.10 15:30:34.809    372818 (EURUSD,M1)    IF ELSE Loop time: 4.0 nanosec; total time: 67479 microsec. Left = 1606418432 Right = 532676575
2021.07.10 15:30:34.830    372818 (EURUSD,M1)    TERNARY ? Loop time: 0.2 nanosec; total time: 3015 microsec. Left = 1606418432 Right = 532676575

I fixed the bugs and added quick checking of the solutions.

Thanks for your time it was funny.

Yes, exactly. The ternary operator is the unbeaten champ.