3 ms·
Actually, in real use, the XOR swapping is almost always slower, because in practice the temp turns into: Read a into register1 Read b into register2
by CJefferson 13y ago
Actually, in real use, the XOR swapping is almost always slower, because in practice the temp turns into:
Read a into register1
Read b into register2
Write register1 to a
Write register2 to b
Whereas with the xor, you read the values, then do some pointless xoring, then write them back out. Further, if you are using a and b for anything else, the 'tmp' turns into a simple register renaming, for 0 cost!
While ^ swapping makes sense in assembler, it never makes sense in C.
- dima55 13y agoDoesn't even makes sense in assembly, at least on x86, since you can just use the xchg instruction.