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:
“There may be other reasons for a mans not speaking in publick than want of resolution: he may have nothing to say.”
—Samuel Johnson (17091784)
“It seems to me that we have to draw the line in sibling rivalry whenever rivalry goes out of bounds into destructive behavior of a physical or verbal kind. The principle needs to be this: Whatever the reasons for your feelings you will have to find civilized solutions.”
—Selma H. Fraiberg (20th century)
“Tax avoidance means that you hire a $250,000-fee lawyer, and he changes the word evasion into the word avoidance.”
—Franklin D. Roosevelt (18821945)
“It is by the goodness of God that in our country we have those three unspeakably precious things: freedom of speech, freedom of conscience, and the prudence never to practice either of them.”
—Mark Twain [Samuel Langhorne Clemens] (18351910)