1

I have been asked a question to swap two numbers without using temporary variable. It was a easy to answer the question with the following answer.

swapping with temp variable

int a =10;
int b =20;
int temp = a;
a = b;
b = temp;

swapping without temp variable

int a =10;
int b =20;
a = a+b;
b = a-b;
a = a-b;

It works, But which is better in terms of performance? When it is only one time operation performance doesn't matter i think. But if i need to reverse a huge array of numbers using this swapping method or during a selection sort where swapping needed etc.., creating local variable is better or doing it with arithmetic operation?

JPS
  • 2,730
  • 5
  • 32
  • 54
  • 4
    Are you asking about Java or Javascript? They're entirely different languages. This looks like Java rather than Javascript - if you're *actually* only interested in Java, please remove the Javascript tag. – Jon Skeet Apr 03 '18 at 10:10
  • 1
    The first one is more readable. It should not influence on performance so much to think about it – Mikita Herasiutsin Apr 03 '18 at 10:12
  • 3
    Swapping without a temp variable as in your posted example is prone to [overflow issues](https://stackoverflow.com/questions/3001836/how-does-java-handle-integer-underflows-and-overflows-and-how-would-you-check-fo). That won't matter in Java, but other languages might have problems. – Andrew Henle Apr 03 '18 at 10:17
  • the latter is like [XOR-swap](https://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_avoidance_in_practice) [which should be avoided](https://softwareengineering.stackexchange.com/q/182037/98103) – phuclv Apr 03 '18 at 10:28

2 Answers2

2

I will say definitely with a temp variable i.e. your first option. If you refer to your second example you have done total three operation. For each operation your java virtual machine generate equivalent assembly operation. But in your first option with a temp variable you are just swapping the values. Hence very minimal assembly operation only to read from address and write into address.

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
  • 1
    Indeed. After optimization, the temp variable is almost certainly elided - `a` is copied to one register, `b` is copied to another. Then `a`'s value is stored in `b` and `b`'s value is stored in `a`. – Andrew Henle Apr 03 '18 at 10:19
1

Quite apart from the fact that the second one can overflow*, there are three possible answers for this:

  1. You can measure it.
  2. Only think about this level of optimisation if you've measured it and this is really proven to be a problem. (In this instance it is highly unlikely that you'll be able to measure any appreciable difference.)
  3. When in doubt, it's usually a good idea to choose the simpler / more canonical solution. A lot of optimisations are based on the premise that you write "normal" code, and will look for patterns developers usually employ. Naive optimisation efforts can sometimes result in inferior performance with Java Hotspot.

So unless you want to dive into the wonderful and tricky (but fun) world of microbenchmarking, just go with the temp variable.

*Though because of the way Java handles overflows, you'll still get the correct result. But as an aside, if you use xor instead of arithmetic operations, there is no danger of overflow.

P.s.: If you are reversing a huge array, the running time will be overwhelmingly dominated by memory access to the array.

biziclop
  • 48,926
  • 12
  • 77
  • 104
  • Could you please give an example of of `a` and `b` values which produce wrong result with the second method? Yes, `a+b` or `a-b` can overflow/underflow, but can this ever produce a wrong result of swapping? – lexicore Apr 03 '18 at 10:22