STL implements a generic std::swap
function to swap 2 values. It can be presented in the following way:
template void swap (T&am
As has already been explained in most scenarios the XOR bitfiddling will be slower.
But it also depends a lot on the surrounding code. Lets say that this swap is being done alone, far away from any other code that requires those values (so they are not loaded into registers) and we are working with "normal" x86 processors here.
Any algorithm that swaps the 2 values will at least need 2 operations to load the values from memory into registers and another 2 operations to store those values to memory again (x86 does not have operations to swap the content of 2 memory-locations directly).
When using a temp-variable like so:
void swap (int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
basically any compiler will recognize that 'temp' is only used locally for the swapping and will not give it a memory-location. And as it only holds the value of 'a' it will not even be a seperate register.
The assembly-code of that will look something like this (pseudo-assembly):
load a to rA
load b to rB
store rA to b
store rB to a
So in most scenarios this would be the most efficient possible in terms of memory-access, number of instructions and number of register.
Only if the compiler fails to recognize that 'temp' is not used for anything else and would store it in a seperate register (or be damned actuall memory) could the XOR-variant be more efficient in anything.
But this is still pruely theoretical cause your swap will be surrounded by other code and that will be far more important there. If the values are not used anyore then the whole swap will be ignored. If the values are used directly after for other computations then it might just be that the following code has 2 registers swapped so the swap it self has 0 instructions. And you will be really hard pressed to find any solution that is more efficient then literally having nothing to do.
And of course there are other more obscure instructionsets that might have instructions to directly swap the content of 2 memory locations.