Possible conditional check error - page 5

 
amrali:

Hi Alain, I have checked your code. With the ternary operator, you are actually comparing apples to oranges. Your code increment the left and right pointers unconditionally, this is reflected on your shorter execution time, unlike the original code, which increment the pointers based on the condition.

Here is my results afer I made some corrections.


Oh ! You are right. That's why the ternary operator was so fast, if fixed it completely lost the advantage of course, which is actually more logical.

Very interesting discussion. Thank you.

 
Alain Verleyen: Oh ! You are right. That's why the ternary operator was so fast, if fixed it completely lost the advantage of course, which is actually more logical. Very interesting discussion. Thank you.

No, it is still the fastest of all of them, even with the bugs fixed.

EDT: The fastest was "TERNARY ? Loop" and in a very close second place was the "BINARY SHIFT APPLES Loop".

 
Fernando Carreiro:

No, it is still the fastest of all of them, even with the bugs fixed.

EDT: The fastest was "TERNARY ? Loop" and in a very close second place was the "BINARY SHIFT APPLES Loop".

Yes but I meant it's no more 20 times faster but rather in the "2 times faster" as the other methods. 

 
Alain Verleyen:

Yes but I meant it's no more 20 times faster but rather in the "2 times faster" as the other methods. 

Interesting topic. Thank you all. 
 
Alain Verleyen: Yes but I meant it's no more 20 times faster but rather in the "2 times faster" as the other methods. 

No, it is still 11 times faster than the "if" versions, and 8 times faster than the "unconditional" version.

 
Fernando Carreiro:

No, it is still 11 times faster than the "if" versions, and 8 times faster than the "unconditional" version.

Please show your fix.

 
Alain Verleyen: Please show your fix.

My fix? I am just reporting on the stats reported by @amrali in his post #40.

Unconcditional Loop: total time:  81974 microsec.
IF IF Loop time:     total time: 110222 microsec.
TERNARY ? Loop time: total time:   9404 microsec.

9404 / 110222 =  8.53%
9404 /  81974 = 11.47%

110222 / 9404 =  11.72
 81974 / 9404 =   8.72
 
Fernando Carreiro:

My fix? I am just reporting on the stats reported by @amrali in his post #40.

You wrote :

No, it is still the fastest of all of them, even with the bugs fixed.

So I misunderstood your point, my original version is bugged as noticed by amrali. It can't be compared with the other versions, the code provided by amrali doesn't fix this specific bug.

With all bugs fixed.

2021.07.11 13:01:46.331 372818 (EURUSD,M1) Unconditional Loop time: 1.3 nanosec; total time: 22113 microsec. Left = 191 Right = 63

2021.07.11 13:01:46.408 372818 (EURUSD,M1) IF IF Loop time: 4.1 nanosec; total time: 68896 microsec. Left = 191 Right = 63

2021.07.11 13:01:46.488 372818 (EURUSD,M1) IF ELSE IF Loop time: 4.3 nanosec; total time: 71620 microsec. Left = 191 Right = 63

2021.07.11 13:01:46.568 372818 (EURUSD,M1) IF ELSE Loop time: 4.2 nanosec; total time: 70442 microsec. Left = 191 Right = 63

2021.07.11 13:01:46.599 372818 (EURUSD,M1) TERNARY ? Loop time: 1.3 nanosec; total time: 22566 microsec. Left = 191 Right = 63

2021.07.11 13:01:46.631 372818 (EURUSD,M1) BINARY SHIFT Loop time: 1.4 nanosec; total time: 23123 microsec. Left = 191 Right = 63

I also improved the original "Unconditional" from @Dominik Egert which is now among the faster (not always the faster as from a run to an other it changes).
Files:
372818.mq5  7 kb
 
Alain Verleyen: So I misunderstood your point, my original version is bugged as noticed by amrali. It can't be compared with the other versions, the code provided by amrali doesn't fix this specific bug.

With all bugs fixed. I also improved the original "Unconditional" from @Dominik Egert which is now among the faster (not always the faster as from a run to an other it changes).

In his post, he wrote "Here is my results afer I made some corrections", so I assumed his stats were based on the corrected version.

Now that you have shown your stats of the "truly corrected" version, I can understand your point.

Thank you!

 
Alain Verleyen:

You wrote :

So I misunderstood your point, my original version is bugged as noticed by amrali. It can't be compared with the other versions, the code provided by amrali doesn't fix this specific bug.

With all bugs fixed.

2021.07.11 13:01:46.331 372818 (EURUSD,M1) Unconditional Loop time: 1.3 nanosec; total time: 22113 microsec. Left = 191 Right = 63

2021.07.11 13:01:46.408 372818 (EURUSD,M1) IF IF Loop time: 4.1 nanosec; total time: 68896 microsec. Left = 191 Right = 63

2021.07.11 13:01:46.488 372818 (EURUSD,M1) IF ELSE IF Loop time: 4.3 nanosec; total time: 71620 microsec. Left = 191 Right = 63

2021.07.11 13:01:46.568 372818 (EURUSD,M1) IF ELSE Loop time: 4.2 nanosec; total time: 70442 microsec. Left = 191 Right = 63

2021.07.11 13:01:46.599 372818 (EURUSD,M1) TERNARY ? Loop time: 1.3 nanosec; total time: 22566 microsec. Left = 191 Right = 63

2021.07.11 13:01:46.631 372818 (EURUSD,M1) BINARY SHIFT Loop time: 1.4 nanosec; total time: 23123 microsec. Left = 191 Right = 63

I also improved the original "Unconditional" from @Dominik Egert which is now among the faster (not always the faster as from a run to an other it changes).

As I said before, this simple loop became so unreadable. You sacrifice readability for a neglible gain in performance. It deos NOT worth it.

A simple if..else statement plus compiler optimizations and hardware branch predictors wins at the end.

At least an order of magnitude (10x) gain in performance is considered good.