1

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?

2
  • 3
    It's not only a bunch of baloney (what's that mean ? not a compliment I guess) it's fragile too - should firstNum + secondNum overflow you're netherwockered. Commented Mar 28, 2019 at 23:21
  • 1
    @HighPerformanceMark bunch of baloney bologna. Commented Mar 28, 2019 at 23:25

4 Answers 4

3

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).

Sign up to request clarification or add additional context in comments.

1 Comment

@MS90 Careful what you wish for.
3

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...

4 Comments

Upvoted for the old-but-good xor exchange. (Next on the agenda: xor linked lists; two-way pointers in the space of one pointer?)
Im rooting for this answer because it calls out the overflow. Just wish you'd mention that the actual purpose of this trick is to teach students about the space time trade off. Sometimes space is tight. Sometimes time is tight. But readability trumps all because if I can't read it I can't make it small or fast.
I believe you and I know what such "tweaks" don't do any good :) I just found a article and wanted to ask you if doing swap that way does any diffrence and you confirmed it even worse.
These days the really big issue is that if you are swapping variables you are probably doing it wrong--as we move toward safer functional style, variables shouldn't change value if at all possible.
1
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 

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.