swap of two variables using temporary variable execute first or without using temporary variable execute first?

3 Years
Discussion Span
Last Post by vegaseat

Could you explain the question a bit better, please? It isn't clear just what you are looking for, especially what you mean by 'execute first'. That last part sounds like your professor's instructions to you rather than a part of the question.

I assume you are asking, how would you write the code for this, but frankly, the first part sounds trivial; you would simply assign the first variable's value to the temporary variable, then assign the second variable's value to the first variable, then finally assign the temp variable's value (which is holding the first's old value, remember) to the second variable.The only possible complication I can see is if you are writing it as a function, in which case you would have to pass the variables by reference (that is to say, using pointers to the actual variables rather than copies of the values).

As for swapping without a temporary variable, the usual approach in C is to use a trick involving the XOR operator (^), but I'll leave it to you to figure it out. I will warn you that the XOR trick isn't necessarily more space efficient than the temp variable version (because of certain optimizations the compiler can use involving internal registers), and it has a potential to backfire - if the two values are already the same, then XORing them will give a result of zero. It's the sort of kludgy optimization that you are unlikely to bother with on a modern system, because the overhead of the alternative is at worst negligible.

Edited by Schol-R-LEA


i am asking which is efficient?

code without using temporary variable:


At that granularity, everything is efficient. However, tricks for swapping without a temporary variable have a higher chance of not being as efficient due to potentially confusing the compiler's optimizer.

The lesson to be learned is that it's not worth trying to optimize a few bytes from a single scalar object. You'll get far better returns on the investment of reducing memory usage from large aggregates or optimizing algorithms with a larger impact on performance.

Also, it's rare when you can achieve both space and speed savings; most of the time you can only get one at the cost of the other.

Edited by deceptikon


I have to agree with Deceptikon on this: the difference in performance between these two approaches is going to be both minor and unpredictable, as the compiler's optimization could get ruined by the cleverness of the hack.

If your concerned about performance, the best thing to do is to start by profiling the unoptimized performance. This will tell you if and where any optimization is needed. Also, you always want to compare the profile results of the 'optimzied' version with the 'unoptimized' version; you may find that your work has actually made things worse rather than better.

Given your interest in optimization, I would highly recommend reading the Graphics Programming Black Book, as it remains the best text on the subject of software optimization I know of despite it's age.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.