-4

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!

Vincent
  • 103
  • 7
  • 3
    And did you try...? And I guess`x < 3` is faster as it's Assembly code is shorter. And the question should be specific to a language. – gdoron Jun 24 '12 at 16:41
  • 2
    These two conditions aren't even equivalent. `0 < 3` but `!((0 == 1) || (0 == 2))`. – Fred Foo Jun 24 '12 at 16:50
  • yeah larsmans and -345 is also < 3. Why would that matter? Why do you assume it's possible for x to have any other value than, say 1, 2, 3, 4 or 5? In which case those statements are perfectly equivalent... – Vincent Jun 25 '12 at 15:14

1 Answers1

8

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 code

if (x < 3) /* action */;

being translated to smth like (for x86 CPU):

  cmp %eax, 3   // <-- compare EAX and 3. It modifies flags register(*1) (namely ZF, CF, SF and so on)
  jge @@skip    // <-- this instruction tells CPU to jump when some conditions related to flags are met(*2).
                // So this code is executed when jump is *not* made (when x is *not*
                // greater or equal than 3.
  /* here is action body */
@@skip:
  // ...

Now consider this code:

if (x == 3) /* action */;

It will give almost the same assembly (of course, it may differ from mine, but not semantically):

  cmp %eax, 3
  jne @@skip // < -- here is the only difference
  /* action is run when x is equal to 3 */
@@skip:
  ...

Both of this operators (jge and jne and 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

Andrew D.
  • 1,022
  • 12
  • 26
  • 3
    +1 for good, verbose, and, above all, correct answer! – David Titarenco Jun 24 '12 at 17:29
  • Thanks so much for this answer man, that's really nice. – Vincent Jun 25 '12 at 14:58
  • So David Titarenco, first you help closing my question as "not constructive" and then when I get the exact answer I was looking for, you congratulate that person. Consistency please. – Vincent Jun 25 '12 at 15:02
  • For the record: you state there is "evident difference in complexity", and I guess that's fair enough. But that's only because of the rest of your answer. If "<" was, say, 4 times more expensive computationally than "==", then this difference might have overridden the cost of multiple, cheaper operators. Which was sort of the point of my question. Of course, if "==" and "<" are equivalent in cost then this is not an issue. Thanks again. – Vincent Jun 25 '12 at 15:16
  • > If "<" was, say, 4 times more expensive computationally than "==" You should be more precise thereafter when formulating your questions (eg. specify language, sometimes platform). Here i assumed this is a beginner's question about C language (if about C++ -- there is more questions then answers, because type matters). And as you agreed and liked it (because there are a lot of disputable points).. so i made a good shot? – Andrew D. Jun 25 '12 at 22:01