News for hackers. Stuff that matters.
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.

There are no comments on this comment yet.