-3

The following pieces of code do the same logic (x is either 0 or 1 only). Which code executes faster ?

First Code:

if (x == 1)
{
   y = 10;
}

Second Code:

if (x != 0)
{
   y = 10;
}

Third Code:

if (x)
{
   y = 10;
}
Hesham Eraqi
  • 2,444
  • 4
  • 23
  • 45
  • 1
    I would say it is the exact same execution time – Charleshaa Aug 28 '14 at 13:11
  • 1
    They are likely the same, but this is CPU and compiler dependent. Why don't you have the compiler generate assembly (`-S` option perhaps), or disassemble the compiled result and check? – lurker Aug 28 '14 at 13:11
  • You edited it incorrectly (should be "x either 1 or 0"). But, any C coder would write `if (x) { ... }` anyway. –  Aug 28 '14 at 13:14
  • @lurker IOW terrible. ``/`bool` is way better. – Bartek Banachewicz Aug 28 '14 at 13:15
  • 1
    @BartekBanachewicz you're assuming that type and header even exists in the OP's compiler. – lurker Aug 28 '14 at 13:16
  • @lurker The question is tagged C, so there's no reason we shouldn't assume C11, which is the current standard. – Bartek Banachewicz Aug 28 '14 at 13:17
  • @BartekBanachewicz I would not assume C11 just because C is tagged. A wide variety of C compile environments, some quite old, are represented in questions on SO. I just commented on another where the OP was using Turbo C 3.0! – lurker Aug 28 '14 at 13:17
  • 2
    @lurker Programming language C is defined by ISO/IEC JTC1/SC22/WG14 standard. The most current standard (ISO/IEC 9899) should be in effect when talking about C, except when explicitely noted otherwise. – Bartek Banachewicz Aug 28 '14 at 13:19
  • 1
    I already tried this code in different platforms and already obtained different performances. @lurker Would you please share your concern about Y ? – Hesham Eraqi Aug 28 '14 at 13:19
  • @BartekBanachewicz "should be" perhaps, but not always in reality here. – lurker Aug 28 '14 at 13:19
  • Related: http://stackoverflow.com/questions/24848359/which-is-faster-while1-or-while2?rq=1 (read the answer that says: look at the assembler. It will probably amount to the same in all 3 cases presented here). –  Aug 28 '14 at 13:19
  • Different performances between the platforms, or between the clauses? –  Aug 28 '14 at 13:20
  • For platform1 I get different clauses performances. For platform2 I get same clauses performances. – Hesham Eraqi Aug 28 '14 at 13:21
  • Is `x` still supposed to be 1 or 2? Your updates haven't changed that sentence. –  Aug 28 '14 at 13:22
  • @HeshamEraqi if you're already able to measure this (as you indicated a different performance observed on different platforms), then why are you asking the question? It clearly depends upon platform and compiler, as I indicated in my first comment. – lurker Aug 28 '14 at 13:23
  • 1
    @lurker By the way, even if the OPs compiler doesn't support C11, there are other people out there reading this question and answers. You shouldn't limit the usability of the answers basing on arbitrary assumptions. [Relevant C++ thread](http://meta.stackexchange.com/questions/112641/when-did-the-c-tag-start-to-imply-c11-by-default) – Bartek Banachewicz Aug 28 '14 at 13:24
  • @lurker You didn't share your concern about Y yet. I wanted to know reasons behind this. – Hesham Eraqi Aug 28 '14 at 13:25
  • @BartekBanachewicz I do agree it's good to indicate what's best for C11 for those using that compiler. My suggestion of `if (x)` would still not be "terrible" in that case, since it would apply even for `bool x;`. – lurker Aug 28 '14 at 13:27
  • @HeshamEraqi I never expressed any concern about `Y`. Evert did. – lurker Aug 28 '14 at 13:27
  • @Evert Sorry it was not intended I meant 0 or 1. What was your concern about Y ? – Hesham Eraqi Aug 28 '14 at 13:28
  • 1
    I wondered if, in case `y = 0` before, the following could be faster: `y = 10*x` (no if-clause needed). –  Aug 28 '14 at 14:03

2 Answers2

2

This depends on an architecture. On many 8-bit microcontrollers comparing to zero requires a single instruction, while comparing to a specific number other than zero requires two instructions.

The first code would translate to this:

LDAA  x
CMPA  #1
BNE   loc
...

This loads x into a register, compares the register to 1, and jumps on equality.

The second code would translate to this:

LDAA  x
BNE   loc

Loading x into the register sets zero flag, so jumping on equality to zero can be done right away. This saves two bytes of code memory, and two CPU cycles.

You can check if that is what's happening on your platform by compiling a small sample to assembly, or by disassembling your actual code.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

You may want to measure it, but most likely those two are exactly the same speedwise (on machine code level they are pretty similar).

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
  • 2
    If x is the result of a previous computation, the compiler can directly jump based on the leftover Zero flag in the second case. In the first case, a comparison is required. – Quentin Aug 28 '14 at 13:15
  • 1
    @Quentin Unless, of course, the compiler knows that x is Boolean. The question implies that it is (otherwise the three options would not be equivalent), though the compiler might not be able to discern/exploit that fact. –  Aug 28 '14 at 15:19