Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a great question, but I think you've fallen victim to the compiler's dependency analysis.</p> <p>The compiler only has to clear the high bits of <code>eax</code> once, and they remain clear for the second version. The second version would have to pay the price to <code>xor eax, eax</code> except that the compiler analysis proved it's been left cleared by the first version.</p> <p><strong>The second version is able to "cheat" by taking advantage of work the compiler did in the first version.</strong></p> <p>How are you measuring times? Is it "(version one, followed by version two) in a loop", or "(version one in a loop) followed by (version two in a loop)"?</p> <p>Don't do both tests in the same program (instead recompile for each version), or if you do, test both "version A first" and "version B first" and see if whichever comes first is paying a penalty.</p> <hr> <p>Illustration of the cheating:</p> <pre><code>timer1.start(); double x1 = 2 * sqrt(n + 37 * y + exp(z)); timer1.stop(); timer2.start(); double x2 = 31 * sqrt(n + 37 * y + exp(z)); timer2.stop(); </code></pre> <p>If <code>timer2</code> duration is less than <code>timer1</code> duration, we don't conclude that multiplying by 31 is faster than multiplying by 2. Instead, we realize that the compiler performed common subexpression analysis, and the code became:</p> <pre><code>timer1.start(); double common = sqrt(n + 37 * y + exp(z)); double x1 = 2 * common; timer1.stop(); timer2.start(); double x2 = 31 * common; timer2.stop(); </code></pre> <p>And the only thing proved is that multiplying by 31 is faster than computing <code>common</code>. Which is hardly surprising at all -- multiplication is far far faster than <code>sqrt</code> and <code>exp</code>.</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