Aaron
Aaron

Reputation: 906

Is an OR statement faster than multiple IF statements?

Let's say I have some code like this:

if (ItemA = nil) then
  Exit;
if (ItemB = '') then
  Exit;

Would it be faster/more efficient to do:

if (ItemA = nil) or (ItemB = '') then
  Exit;

I realize that's most likely something that won't impact performance measurably, but I'm just curious.

Upvotes: 1

Views: 260

Answers (1)

David Heffernan
David Heffernan

Reputation: 613013

They are the same. Here's my test program:

{$APPTYPE CONSOLE}

var
  i, j: Integer;

begin
  if (i=0) then
    Exit;
  if (j=0) then
    Exit;
  if (i=0) or (j=0) then
    Exit;
  Writeln('stop the compiler optimising away the final exit');
end.

And the compiled code from a release build with the 32 bit compiler:

Project1.dpr.9: if (i=0) then
004060D5 85DB             test ebx,ebx
004060D7 7426             jz $004060ff
Project1.dpr.11: if (j=0) then
004060D9 85C0             test eax,eax
004060DB 7422             jz $004060ff
Project1.dpr.13: if (i=0) or (j=0) then
004060DD 85DB             test ebx,ebx
004060DF 741E             jz $004060ff
004060E1 85C0             test eax,eax
004060E3 741A             jz $004060ff

And from the 64 bit compiler:

Project1.dpr.9: if (i=0) then
000000000040B49F 833D2A78000000   cmp dword ptr [rel $0000782a],$00
000000000040B4A6 743B             jz Project1 + $63
Project1.dpr.11: if (j=0) then
000000000040B4A8 833D2578000000   cmp dword ptr [rel $00007825],$00
000000000040B4AF 7432             jz Project1 + $63
Project1.dpr.13: if (i=0) or (j=0) then
000000000040B4B1 833D1878000000   cmp dword ptr [rel $00007818],$00
000000000040B4B8 7429             jz Project1 + $63
000000000040B4BA 833D1378000000   cmp dword ptr [rel $00007813],$00
000000000040B4C1 7420             jz Project1 + $63

As you can see, both of the Windows compilers produce identical code for both variants. I cannot vouch for the mobile compilers, but I'd be astounded if they were any different.

Do feel free to use whichever feels easier to read!

Note that if you enable the complete boolean evaluation option (doc link 1, doc link 2), then things do of course change. The output is now:

Project1.dpr.9: if (i=0) then
004060D4 85C0             test eax,eax
004060D6 742B             jz $00406103
Project1.dpr.11: if (j=0) then
004060D8 85D2             test edx,edx
004060DA 7427             jz $00406103
Project1.dpr.13: if (i=0) or (j=0) then
004060DC 85C0             test eax,eax
004060DE 0F94C0           setz al
004060E1 85D2             test edx,edx
004060E3 0F94C2           setz dl
004060E6 0AC2             or al,dl
004060E8 7519             jnz $00406103

I think it is obvious that complete boolean evaluation will result in slower code. Of course, complete boolean evaluation is something of a corner case since I'd be astounded to find any scenario where it was used. And of course it is logically different from your two if statement version of the code in case the expression being tested has side-effects.

Upvotes: 6

Related Questions