SRL Archive - Discussions and Debates

Small Test


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.


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

R0b0t1

2008-05-05 19:08:04

That's why I've never bought in to the 'Stepping cases are faster!'… They are, if you expect to fail a lot.

drizzt

2008-05-05 20:52:35

hmm, that's interesting, maybe try xor too and post the result? (i think xor is like "or", but, if its not, then just totally disregard that last statement)

Dan Cardin

2008-05-05 22:22:04

xor is a math function

and are you trying to prove that if not is faster or vice versa, or not at all?:p

mixster

2008-05-06 02:34:59

Did you try with if not RMs[0] and not RMs[1] etc. as that should exit out when it finds a false value (or true due to the not)?

Sir R. M8gic1an

2008-05-06 06:12:03

i think you didn-t see the second part mick, look more closely ;)


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.

drizzt, xor is not what you think it is,

(True xor True) = False
(False xor False) = False
(True xor False) = True
(False xor True) = True

it returns true if they are different. and for maths they work another way.

Dan i was trying to prove if not is faster, but was surprised by the result :p i still believe it'll be faster in most cases because we'll always put the ones with most chance of succeeding first. and i'll try later with color analizes because then it may make a bigger difference even if the one that returns true is one of the last ones to be called.

-RM

n3ss3s

2008-05-06 07:07:08

Xor, Extensive or, is a logical operator, not a mathematic function, and works as Rasta said. A good example situation is setting run to true for example, or toggling retaliate.

mixster

2008-05-06 10:35:53

No I mean in one line as it should act the same as that, but I think it may run through all the values anyway if or does too, so I mean literally try:

if((not(RMs[0])) and (not(RMs[1])) and (not(RMs[2])) and (not(RMs[3])) and (not(RMs[4])) and (not(RMs[5])) and (not(RMs[6])) and (not(RMs[7])) and (not(RMs[8]))) then
:)

Sir R. M8gic1an

2008-05-06 12:14:00

mixstrer i think that will will work just like or and lag more because of the "not" but i'll try it once i get home.

and i see you think the same as me but there's no harm in trying :p

~RM

drizzt

2008-05-06 19:19:13

thats why i put it in parenthesis, because i knew i didn't know what i was talking about, but good find rasta

n3ss3s

2008-05-07 10:47:05

SRL Library Small Tuts about SRL-Functions in here…


Erm…?

Sir R. M8gic1an

2008-05-07 11:57:42

From: n3ss3s
Erm…?



find me a better place n ill know next time :) and it is a small tut because we learn in which case a method is faster than the other.

~RM

n3ss3s

2008-05-07 13:28:31

about SRL-Functions in here…


Or is a logical operator, not an SRL function…

If you see this, read my pm…

R0b0t1

2008-05-07 21:00:15

Lol?


I bet 'and', the so useful function, and 'not', was also made by Wizzup? and WT-Fakawi!

mixster

2008-05-13 02:27:45

You think they're that great? Well, you would be right, though their most famous one was repeat until(false) :)