4 ms·
If you do the naive implementation: int temp = x; x = y; y = temp; Then a sufficiently smart compiler can decide to assign, say, %eax to x and %ebx
by rpearl 15y ago
If you do the naive implementation:
int temp = x;
x = y;
y = temp;
Then a sufficiently smart compiler can decide to assign, say, %eax to x and %ebx to y, and then just rename its notion of registers, and after the swap just begin using %ebx for x and %eax for y. A swap with no copies at all!
(It won't do this in every case, and it depends on context, but such an optimization is possible... but not with xor swaps.)
- eru 15y agoAt least xor swaps would need an even smarter compiler.