Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here are some concrete numbers, on Windows using GCC 4.7.2:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main() { unsigned int result = 0; for (int n = -500000000; n != 500000000; n++) { int d = -1; for (int i = 0; i != ITERATIONS; i++) d &amp;= rand(); #if CHECK == 0 if (d == 0) result++; #elif CHECK == 1 result += n / d; #elif CHECK == 2 result += n / (d + !d); #elif CHECK == 3 result += d == 0 ? 0 : n / d; #elif CHECK == 4 result += d == 0 ? 1 : n / d; #elif CHECK == 5 if (d != 0) result += n / d; #endif } printf("%u\n", result); } </code></pre> <p>Note that I am intentionally not calling <code>srand()</code>, so that <code>rand()</code> always returns exactly the same results. Note also that <code>-DCHECK=0</code> merely counts the zeroes, so that it is obvious how often appeared.</p> <p>Now, compiling and timing it various ways:</p> <pre><code>$ for it in 0 1 2 3 4 5; do for ch in 0 1 2 3 4 5; do gcc test.cc -o test -O -DITERATIONS=$it -DCHECK=$ch &amp;&amp; { time=`time ./test`; echo "Iterations $it, check $ch: exit status $?, output $time"; }; done; done </code></pre> <p>shows output that can be summarised in a table:</p> <pre><code>Iterations → | 0 | 1 | 2 | 3 | 4 | 5 -------------+------------------------------------------------------------------- Zeroes | 0 | 1 | 133173 | 1593376 | 135245875 | 373728555 Check 1 | 0m0.612s | - | - | - | - | - Check 2 | 0m0.612s | 0m6.527s | 0m9.718s | 0m13.464s | 0m18.422s | 0m22.871s Check 3 | 0m0.616s | 0m5.601s | 0m8.954s | 0m13.211s | 0m19.579s | 0m25.389s Check 4 | 0m0.611s | 0m5.570s | 0m9.030s | 0m13.544s | 0m19.393s | 0m25.081s Check 5 | 0m0.612s | 0m5.627s | 0m9.322s | 0m14.218s | 0m19.576s | 0m25.443s </code></pre> <p>If zeroes are rare, the <code>-DCHECK=2</code> version performs badly. As zeroes start appearing more, the <code>-DCHECK=2</code> case starts performing significantly better. Out of the other options, there really isn't much difference.</p> <p>For <code>-O3</code>, though, it is a different story:</p> <pre><code>Iterations → | 0 | 1 | 2 | 3 | 4 | 5 -------------+------------------------------------------------------------------- Zeroes | 0 | 1 | 133173 | 1593376 | 135245875 | 373728555 Check 1 | 0m0.646s | - | - | - | - | - Check 2 | 0m0.654s | 0m5.670s | 0m9.905s | 0m14.238s | 0m17.520s | 0m22.101s Check 3 | 0m0.647s | 0m5.611s | 0m9.085s | 0m13.626s | 0m18.679s | 0m25.513s Check 4 | 0m0.649s | 0m5.381s | 0m9.117s | 0m13.692s | 0m18.878s | 0m25.354s Check 5 | 0m0.649s | 0m6.178s | 0m9.032s | 0m13.783s | 0m18.593s | 0m25.377s </code></pre> <p>There, check 2 has no drawback compared the other checks, and it does keep the benefits as zeroes become more common.</p> <p>You should really measure to see what happens with your compiler and your representative sample data, though.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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