[Solved]Speed optimisation

I am currently trying to get my code execution to be as fast as possible. And I saw this online :
They advice to replace :

if (a == b && c == d && e == f ) 

by

if ( ((a-b)|(c-d)|(e-f)) == 0 )

However the conditions are no longer respected when translated that way? I mean the first code asks for every condition to be satisfied while the second one asks for only one out of the three to be true, am I wrong? If I am not is :

if ( ((a-b)+(c-d)+(e-f)) == 0 )

faster than the first line of code?

The "&&" operator stops evaluation if the first FALSE condition is found. That's faster as long as not all conditions are TRUE.

No. The condition is ... == 0 which is determined after the entire calculation. The calculation is done for all three differences because the bitwise "|" operator simply calculates, while logical "||" would treat each single non-zero difference as TRUE and then stops evaluation immediately.

That's wrong because a positive difference can be compensated by a negative difference.

1 Like

Thank you very much for your help!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.