XOR Swap Algorithm - Reasons For Avoidance in Practice

Reasons For Avoidance in Practice

Most modern compilers can optimize away the temporary variable in the naive swap, in which case the naive swap uses the same amount of memory and the same number of registers as the XOR swap and is at least as fast, and often faster. The XOR swap is also much less readable and completely opaque to anyone unfamiliar with the technique.

On modern CPU architectures, the XOR technique is considerably slower than using a temporary variable to do swapping. One reason is that modern CPUs strive to execute instructions in parallel via instruction pipelines. In the XOR technique, the inputs to each operation depend on the results of the previous operation, so they must be executed in strictly sequential order. If efficiency is of tremendous concern, it is advised to test the speeds of both the XOR technique and temporary variable swapping on the target architecture.

Read more about this topic:  XOR Swap Algorithm

Famous quotes containing the words reasons for, reasons, avoidance and/or practice:

    Could truth perhaps be a woman who has reasons for not permitting her reasons to be seen? Could her name perhaps be—to speak Greek—Baubo?... Oh, those Greeks! They understood how to live: to do that it is necessary to stop bravely at the surface, the fold, the skin, to adore the appearance, to believe in forms, in tones, in words, in the whole Olympus of appearance! Those Greeks were superficial—out of profundity!
    Friedrich Nietzsche (1844–1900)

    One who is publicly honest about himself ends up by priding himself somewhat on this honesty: for he knows only too well why he is honest—for the same reasons another person prefers illusion and dissimulation.
    Friedrich Nietzsche (1844–1900)

    Real good breeding, as the people have it here, is one of the finest things now going in the world. The careful avoidance of all discussion, the swift hopping from topic to topic, does not agree with me; but the graceful style they do it with is beyond that of minuets!
    Thomas Carlyle (1795–1881)

    Those who make a practice of comparing human actions are never so perplexed as when they try to see them as a whole and in the same light; for they commonly contradict each other so strangely that it seems impossible that they have come from the same shop.
    Michel de Montaigne (1533–1592)