|Use this form when 'a' and 'b' are likely to be in memory.|
|Use this form when 'a' and 'b' are likely to be in registers.|
The first form uses the classic load-store architecture. We load the variables from the cache, we do some work, we write the variables back into the cache. Great.
The second form is for when you are register starved. We're in a tight loop, and trying to squeeze as much into the instruction cache. Let's use the ALU to do all the work for us. Again, Great!
But it turns out that for all that time, I was completely wrong. The right way to exchange variables is actually this :
|Lets optimize for developer time.|
(Are you still doing it the one of other ways, I'd love to hear from you in the comments!)
But can we do better even than std::swap? Why yes, it turns out we can :
|Wait, that's not C++ !|
Great! So what else can we do?
Maybe it's time for us to reconsider what it is we're optimizing...