This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: swap (a, b) - test
On Sat, Jan 03, 1998 at 08:12:51PM -0800, Richard Henderson wrote:
> If it is faster at all I'll warrent it is only so for depressingly
> register starved machines.
Well, I'm on an Intel 486 - that probably qualifies.
Now, I *did* get great advantage from this before, in more complex
code (quicksort), under gcc-2.7.2.3.
However, a quick test now is now running slower with the XOR swap. I
don't see why three XOR operations on two ints would be slower
than three assignments among three ints...
My test code (included below) ran about 35% faster with the old swap.
Maybe this isn't Nobel-prize material after all :)
-Eric
------------ test code -----------------
#include <stdlib.h>
inline void
swap1 (int & a, int & b)
{
int tmp = a; a = b; b = tmp;
}
inline void
swap2 (int & a, int & b)
{
if (&a != &b) { a ^= b; b ^= a; a ^= b; }
}
int
main (int argc, char** argv)
{
int data [10240];
for (int i=0; i < 10240; i++) data[i] = rand();
for (int j=0; j < 10240; j++)
for (int i=0; i < 10239; i++)
#ifndef FAST_SWAP
swap1 (data[i], data[i+1]);
#else
swap2 (data[i], data[i+1]);
#endif
}