Note that there are some explanatory texts on larger screens.

plurals
  1. POSlow XOR operator
    primarykey
    data
    text
    <p>EDIT: Indeed, I had a weird error in my timing code leading to these results. When I fixed my error, the smart version ended up faster as expected. My timing code looked like this:</p> <pre><code>bool x = false; before = now(); for (int i=0; i&lt;N; ++i) { x ^= smart_xor(A[i],B[i]); } after = now(); </code></pre> <p>I had done the <code>^=</code> to discourage my compiler from optimizing the for-loop away. But I think that the <code>^=</code> somehow interacts strangely with the two xor functions. I changed my timing code to simply fill out an array of the xor results, and then do computation with that array outside of the timed code. And that fixed things.</p> <p>Should I delete this question?</p> <p>END EDIT</p> <p>I defined two C++ functions as follows:</p> <pre><code>bool smart_xor(bool a, bool b) { return a^b; } bool dumb_xor(bool a, bool b) { return a?!b:b; } </code></pre> <p>My timing tests indicate that <code>dumb_xor()</code> is slightly faster (1.31ns vs 1.90ns when inlined, 1.92ns vs 2.21ns when not inlined). This puzzles me, as the <code>^</code> operator should be a single machine operation. I'm wondering if anyone has an explanation.</p> <p>The assembly looks like this (when not inlined):</p> <pre><code> .file "xor.cpp" .text .p2align 4,,15 .globl _Z9smart_xorbb .type _Z9smart_xorbb, @function _Z9smart_xorbb: .LFB0: .cfi_startproc .cfi_personality 0x3,__gxx_personality_v0 movl %esi, %eax xorl %edi, %eax ret .cfi_endproc .LFE0: .size _Z9smart_xorbb, .-_Z9smart_xorbb .p2align 4,,15 .globl _Z8dumb_xorbb .type _Z8dumb_xorbb, @function _Z8dumb_xorbb: .LFB1: .cfi_startproc .cfi_personality 0x3,__gxx_personality_v0 movl %esi, %edx movl %esi, %eax xorl $1, %edx testb %dil, %dil cmovne %edx, %eax ret .cfi_endproc .LFE1: .size _Z8dumb_xorbb, .-_Z8dumb_xorbb .ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3" .section .note.GNU-stack,"",@progbits </code></pre> <p>I'm using g++ 4.4.3-4ubuntu5 on an Intel Xeon X5570. I compiled with -O3.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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