Re:Swapping two Numbers without Temporary Variables
by happytoexplain on Thursday March 24, 2022 @03:45PM
(#30794497)
The author explicitly states that it should never be used due to readability. I'm interested in your point about performance, though - as a layman in this area, I would have thought that the additional operations were cheaper than whatever the overhead of a variable is, even for a primitive value type. Is that definitely not the case?
[ Parent
| on HN ]
Re:Swapping two Numbers without Temporary Variables
by Khoth on Friday March 25, 2022 @07:09AM
(#30800683)
A modern CPU makes heavy use of out of order execution to do things quickly. Having the results of one operation depend on another makes it harder for the CPU to do this.
If you have this:
a = a + b
b = a - b
a = a - b
// use a for something
c = a + 7
then the final value of 'a' depends on both 'a' and 'b', so execution of the final line can't happen until the original 'a' and 'b' have both been fetched from memory/calculated/whatever.However, if you have this:
temp = b
b = a
a = temp
// use a for something
c = a + 7
then the final value of 'a' depends only on 'b', so the final line can be executed even if the original 'a' isn't yet available.
Re:Swapping two Numbers without Temporary Variables
by pjscott on Thursday March 24, 2022 @03:54PM
(#30794614)
In most cases swapping two variables with a temporary variable will be either zero-overhead -- because you've just changed the names you're referring to those variables by, and the compiler knows it -- or very, very low-overhead. You're not actually saving any memory with the fancy tricks.