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:

    Write to the point: say immediately what you want to say most, even if it doesn’t “come first.” There are three reasons for doing this. First, you will then have said it, even if nothing else gets said. Second, your readers will then have read it, even if they read no more. Third, having said it, you are likely to have to say something more, because you will have to explain and justify what you chose to say.
    Bill Stott (b. 1940)

    From things that have happened and from things as they exist and from all things that you know and all those you cannot know, you make something through your invention that is not a representation but a whole new thing truer than anything true and alive, and you make it alive, and if you make it well enough, you give it immortality. That is why you write and for no other reason that you know of. But what about all the reasons that no one knows?
    Ernest Hemingway (1899–1961)

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

    It is not always possible to predict the response of a doting Jewish mother. Witness the occasion on which the late piano virtuoso Oscar Levant telephoned his mother with some important news. He had proposed to his beloved and been accepted. Replied Mother Levant: “Good, Oscar, I’m happy to hear it. But did you practice today?”
    Liz Smith (20th century)