Solution A:
Solution A introduces an additional auxiliary variable called "temp" in the code to hold one of the values (either a or b) which increases the footprint of the compiled class.
temp = a;
a = b;
b = temp;
Solution B:
Solution B on the other side swaps two numbers using bitwise operation. The XOR (Exclusive OR) is introduced in this code.
a = a ^ b;
b = b ^ a;
a = a ^ b;
Solution C:
Last but not least, Solution C swaps two numbers using arithmetic operations, both addition and subtraction.
a = a + b;
b = a - b;
a = a - b;
Conclusion: In terms of speed, I would personally rank Solution B being the fastest, Solution C the second fastest and Solution A the slowest. (B -> C -> A)
0 comments:
Post a Comment