Sir R. M8gic1an
2008-05-05 17:30:33
hey, i looked somewhere earlier where someone had posted a solution to a problem full of "if … or … or … or … or … or … or …" and got curious about how efficient that was.
My conclusion: if or will always check all the cases, no matter if it already found a true one. if not takes a lil longer if the only true case is somewhere towards the end of the list, but is much faster if it is at the beginning. now in this test i only compare booleans, but if we're scanning for colors or something like that it makes a real difference.
have a good look at why the x is important.
and with the if not method we always put those we expect to be the most recurring ones on top.
i was curious about it, and i guess that this might be useful information for some people :)
~RM
My conclusion: if or will always check all the cases, no matter if it already found a true one. if not takes a lil longer if the only true case is somewhere towards the end of the list, but is much faster if it is at the beginning. now in this test i only compare booleans, but if we're scanning for colors or something like that it makes a real difference.
Successfully compiled (88 ms)
x = 0
or or or method took 297 ms
if not method took 94 ms
x = 1
or or or method took 296 ms
if not method took 375 ms
Successfully executed
have a good look at why the x is important.
program SmallTest;
var
RMs: Array[0..8] of Boolean;
found: boolean;
i, t, x:integer;
begin
for x:=0 to 1 do
begin
writeln('x = '+IntToStr(x));
if x=0 then
RMs[0] := True
else
RMs[0] := False;
RMs[1] := False;
RMs[2] := False;
RMs[3] := False;
RMs[4] := False;
RMs[5] := False;
if x=0 then
RMs[6] := False
else
RMs[6] := True;
RMs[7] := False;
RMs[8] := False;
t:=GetSystemTime;
for i:=0 to 10000 do
if RMs[0] or RMs[1] or RMs[2] or RMs[3] or RMs[4] or RMs[5] or RMs[6] or RMs[7] or RMs[8] then
Continue;
writeln('or or or method '+'took '+IntToStr(GetSystemTime-t)+' ms');
t:= GetSystemTime;
for i:=0 to 10000 do
begin
found:=True;
if not RMs[0] then
if not RMs[1] then
if not RMs[2] then
if not RMs[3] then
if not RMs[4] then
if not RMs[5] then
if not RMs[6] then
if not RMs[7] then
if not RMs[8] then
found:=False;
end;
writeln('if not method '+'took '+IntToStr(GetSystemTime-t)+' ms');
end;
end.
and with the if not method we always put those we expect to be the most recurring ones on top.
i was curious about it, and i guess that this might be useful information for some people :)
~RM