Reputation: 22595
In the depths of the Internet I found the following post:
Swapping two numbers without using a new variable is always a good approach. This helps your application to be memory and performance oriented.
And it proposes to use the following code:
int firstNum = 10;
int secondNum = 20;
firstNum = firstNum + secondNum;
secondNum = firstNum - secondNum;
firstNum = firstNum - secondNum;
instead of using a temporary variable.
To be honest it sounds for me like a bunch of baloney. I know, that in a real environment such microtweaks wouldn't do almost any difference, but what intrigues me is, if avoiding using a new variable, in this case, would do any difference?
Upvotes: 1
Views: 107
Reputation: 62769
Not only is it a horrible idea, it's not even a good implementation of a horrible idea! The old trick went:
a^=b;
b^=a;
a^=b;
That one won't have the under/overflow problems and will really confuse your co workers extra good. I mean if you are going for confusing, underperforming code... go all the way!
By the way, I'd generally say with java if you think you might get some performance benefit from doing something in a slightly less obvious way, never do that. First of all you are probably wrong and your solution isn't faster. Secondly java is already hella-fast and it probably doesn't matter. Thirdly java will, over time, improve the "Obvious" ways and make them faster using the runtime compiler. It will not improve your hack and may even make it slower.
If you don't believe me, you probably also still believe you should use stringbuilder every time you concatenate two strings...
Upvotes: 3
Reputation: 3144
public class SwapTest {
int firstNum = 10;
int secondNum = 20;
public static void main(String args[])
{
SwapTest swap2Numbers = new SwapTest();
long before = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
swap2Numbers.proceedNoInterimVariable();
}
System.out.println(" no temp variable took " + (System.currentTimeMillis()-before));
before = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
swap2Numbers.proceedWithInterimVariable();
}
System.out.println("with temp variable took " + (System.currentTimeMillis()-before));
}
private void proceedNoInterimVariable()
{
firstNum = firstNum + secondNum;
secondNum = firstNum - secondNum;
firstNum = firstNum - secondNum;
}
private void proceedWithInterimVariable()
{
int temp = firstNum;
firstNum = secondNum;
secondNum = temp;
}
}
From this on my system the temp variable version performs much faster.
no temp variable took 11
with temp variable took 4
Upvotes: 1
Reputation: 337
Don't think it matters, if anything doing it without a temp variable is 3 operations whereas doing it with the temp variable only is two operations.
Upvotes: 1
Reputation: 201439
It's a bunch of baloney; such a tip was only of any value on early computers (which had severe register limitations). On modern computers, that is not an issue. Use the temporary variable (prefer more readable code).
Upvotes: 3