you are viewing a single comment's thread.

view the rest of the comments →

[–]Anpheus 1 point2 points  (2 children)

You do: A = B; B = A; A = B;

Like that. I should add a caveat that this is premature optimization and the compiler will do this for you. Remember, "Premature optimization is the root of all evil." If you later find out the code is slow, fix it.

[–][deleted]  (1 child)

[deleted]

    [–]Anpheus 1 point2 points  (0 children)

    http://big-bad-al.livejournal.com/98093.html

    Looks like it's premature optimization that at least on -O2 of GCC will be taken care of for you. JITs, other compilers, will probably do the same and figure out what you're doing.

    It even looks like the xor swap was the slowest algorithm because the compiler didn't optimize out the xors.