- Apr 23, 2001
- 1,658
- 0
- 0
if ( A==B || A==C || A==D)
...
endif
if CompareVars(a,b,c,d) = 1 then
...
end if
function CompareVars(a, b, c, d)
if a <> b return 0
if a <> c return 0
if a <> d return 0
return 1
end function
Cogman,
What language. I'd think that he java JVM would optimize this at runtime to do what you said.
All you can really do is put the likelihood of failure in order. This is compiler dependent for C/C++ but the Java spec says left to right processing order.
So, for Java, you would put the expression most likely to fail first and least likely last. This way, the case where the expression returns false is calculated in the fastest average time.
This is a micro-optimization though. In the real world, it is not worth the time it takes to think about it.
Could someone explain the difference between OP and Cogman's code... All I see is that Cogman removed three of the pipes.
Assuming a C/C++ like language, My code uses bitwise ors, the OPs uses logical ors. That means that mine instructs the compiler to just or the results of each equality together while to ops tells the compiler that it must first find the logical equivalent of the results of the equalities before taking the bitwise or. In other words, it has to convert a non 1/0 value into a 1/0 value. (even though the equalities are only capable of returning a 1/0 value).Could someone explain the difference between OP and Cogman's code... All I see is that Cogman removed three of the pipes.
if ( A==B || A==C || A==D)
...
endif
if( A==B ) goto doit;
if( A==C ) goto doit;
if( A==D ) goto doit;
goto dontdoit;
:doit
...
:dontdoit
There's another difference. When testing (x|y|z), all the results have to be computed and or-ed together. When testing (x||y||z), if x is true, y and z don't matter, and so they may not be touched at all. In general with C-like languages:
Code:if ( A==B || A==C || A==D) ... endif
is equivalent to
Code:if( A==B ) goto doit; if( A==C ) goto doit; if( A==D ) goto doit; goto dontdoit; :doit ... :dontdoit
There's another difference. When testing (x|y|z), all the results have to be computed and or-ed together. When testing (x||y||z), if x is true, y and z don't matter, and so they may not be touched at all. In general with C-like languages:
Code:if ( A==B || A==C || A==D) ... endif
is equivalent to
Code:if( A==B ) goto doit; if( A==C ) goto doit; if( A==D ) goto doit; goto dontdoit; :doit ... :dontdoit