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:

    I should like to know what is the proper function of women, if it is not to make reasons for husbands to stay at home, and still stronger reasons for bachelors to go out.
    George Eliot [Mary Ann (or Marian)

    As if her velvet helmet high
    Did turret rationality.
    She fans her wing up to the winde
    As if her Pettycoate were lin’de
    With reasons fleece, and hoises saile
    And humming flies in thankfull gaile
    Edward Taylor (1645–1729)

    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)

    We black women must forgive black men for not protecting us against slavery, racism, white men, our confusion, their doubts. And black men must forgive black women for our own sometimes dubious choices, divided loyalties, and lack of belief in their possibilities. Only when our sons and our daughters know that forgiveness is real, existent, and that those who love them practice it, can they form bonds as men and women that really can save and change our community.
    Marita Golden, educator, author. Saving Our Sons, p. 188, Doubleday (1995)