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:

    One of the many reasons for the bewildering and tragic character of human existence is the fact that social organization is at once necessary and fatal. Men are forever creating such organizations for their own convenience and forever finding themselves the victims of their home-made monsters.
    Aldous Huxley (1894–1963)

    The needs of a human being are sacred. Their satisfaction cannot be subordinated either to reasons of state, or to any consideration of money, nationality, race, or color, or to the moral or other value attributed to the human being in question, or to any consideration whatsoever.
    Simone Weil (1909–1943)

    The American Dream, the idea of the happy ending, is an avoidance of responsibility and commitment.
    Jill Robinson (b. 1936)

    In the case of all other sciences, arts, skills, and crafts, everyone is convinced that a complex and laborious programme of learning and practice is necessary for competence. Yet when it comes to philosophy, there seems to be a currently prevailing prejudice to the effect that, although not everyone who has eyes and fingers, and is given leather and last, is at once in a position to make shoes, everyone nevertheless immediately understands how to philosophize.
    Georg Wilhelm Friedrich Hegel (1770–1831)