Note that there are some explanatory texts on larger screens.

plurals
  1. POint operators != and == when comparing to zero
    text
    copied!<p>I've found that != and == are not the fastest ways for testing for zero or non-zero.</p> <pre><code>bool nonZero1 = integer != 0; xor eax, eax test ecx, ecx setne al bool nonZero2 = integer &lt; 0 || integer &gt; 0; test ecx, ecx setne al bool zero1 = integer == 0; xor eax, eax test ecx, ecx sete al bool zero2 = !(integer &lt; 0 || integer &gt; 0); test ecx, ecx sete al </code></pre> <p>Compiler: VC++ 11 Optimization flags: /O2 /GL /LTCG</p> <p>This is the assembly output for x86-32. The second versions of both comparisons were ~12% faster on both x86-32 and x86-64. However, on x86-64 the instructions were identical (first versions looked exactly like the second versions), but the second versions were still faster.</p> <ol> <li>Why doesn't the compiler generate the faster version on x86-32?</li> <li>Why are the second versions still faster on x86-64 when the assembly output is identical?</li> </ol> <p><strong>EDIT: I've added benchmarking code. ZERO: 1544ms, 1358ms NON_ZERO: 1544ms, 1358ms</strong> <a href="http://pastebin.com/m7ZSUrcP" rel="noreferrer">http://pastebin.com/m7ZSUrcP</a> or <a href="http://anonymouse.org/cgi-bin/anon-www.cgi/http://pastebin.com/m7ZSUrcP" rel="noreferrer">http://anonymouse.org/cgi-bin/anon-www.cgi/http://pastebin.com/m7ZSUrcP</a></p> <p>Note: It's probably inconvenient to locate these functions when compiled in a single source file, because main.asm goes quite big. I had zero1, zero2, nonZero1, nonZero2 in a separate source file.</p> <p><strong>EDIT2: Could someone with both VC++11 and VC++2010 installed run the benchmarking code and post the timings? It might indeed be a bug in VC++11.</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload