Alex Rivera | Logout

Why differ(!=,<>) is faster than equal(=,==)?

Asked 2009-07-14T10:15:05.670
9

I've seen comments on SO saying "<> is faster than =" or "!= faster than ==" in an if() statement.

I'd like to know why is that so. Could you show an example in asm?

Thanks! :)

EDIT:

Source

Here is what he did.

  function Check(var MemoryData:Array of byte;MemorySignature:Array of byte;Position:integer):boolean;
   var i:byte;
   begin
    Result := True; //moved at top. Your function always returned 'True'. This is what you wanted?
    for i := 0 to Length(MemorySignature) - 1 do //are you sure??? Perhaps you want High(MemorySignature) here... 
    begin
{!}  if MemorySignature[i] <> $FF then //speedup - '<>' evaluates faster than '='
     begin
      Result:=memorydata[i + position] <> MemorySignature[i]; //speedup.
      if not Result then 
        Break; //added this! - speedup. We already know the result. So, no need to scan till end.
     end;
    end;
   end;
Edit
Report

1 Answer

19

I'd claim that this is flat out wrong except perhaps in very special circumstances. Compilers can refactor one into the other effortlessly (by just switching the if and else cases).

answered 2009-07-14T10:19:31.573

Your Answer