if premature optimization is the root of all evil then optimizing without measuring is the square root of all evil.

if premature optimization is the root of all evil then optimizing without measuring is the square root of all evil.

compare this "highly optimized" one with the old age and boring way using a temp. variable

procedure ExchangeInteger(var AValue1: Integer; var AValue2: Integer);
begin
  AValue1 := AValue1 xor AValue2;
  AValue2 := AValue1 xor AValue2;
  AValue1 := AValue1 xor AValue2;
end;

But, hey, the collegue has proven he knows some tricks.

Comments

  1. This was from the days you had processors lacking an exchange instruction, and lacking lots of registers (like the 6502).

    ReplyDelete
  2. Jeroen Pluimers Exactly the processor of which I was thinking. ;)  However, it is not only a paucity of registers which provokes this usage, but a generally greater facility with binary manipulations than we cultivate when we are further removed frmo the metal.

    ReplyDelete
  3. I've scheduled a blog entry about this topic for mid December (:
    with a reference to this thread and http://en.wikipedia.org/wiki/XOR_swap_algorithm

    ReplyDelete

Post a Comment