This is really just sort of an academic question, I’m just curious to know which one is faster. I’m guessing the difference is negligible but still, I’d like to know.
if( (x == 1) || (x == 2) )
vs
if (x < 3)
thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the form you provided there is evident difference in complexity: the first code uses 3 operators, then the second — just one. But OK, lets put this code of and assume you want to compare
>(or<) and==(!=). If you have ever faced assembler while examing your programs (but i bet you didn’t) you would notice such codebeing translated to smth like (for x86 CPU):
Now consider this code:
It will give almost the same assembly (of course, it may differ from mine, but not semantically):
Both of this operators (
jgeandjneand others jumps) do their job with the same speed (because CPUs are made so, but it obviously depends on its architecture). The more impact on performance does distance of jump (difference between code positions), cache misses (when processor wrongly predicted the jump), and so on. Some instructions are more effective even (use less bytes for example), but remember the only thing: do not pay them so much attention. It always better to make algorythmic optimizations, don’t do savings on matches. Let the compiler do it for you — it is really more competent in such questions. Focus on your algorythms, code readablity, program architecture, fault-tolerance.. Make speed the last factor.(*1): http://en.wikipedia.org/wiki/FLAGS_register_%28computing%29
(*2): http://www.unixwiz.net/techtips/x86-jumps.html