# Optimization: Nested If-statements Or one long if ... and ... and ...

## Recommended Posts

I would like to know which one of these codes runs faster as an .exe program(compiled .au3 file):

if \$a = \$b and \$a = \$c and \$a = \$d Then
~to be implemented~
endif

if \$a = \$b then
if \$a = \$c then
if \$a = \$d then
~to be implemented~
endif
endif
endif

##### Share on other sites

I'd be surprised if there was a difference in speed between the two :-)

##### Share on other sites

I would like to know which one of these codes runs faster as an .exe program(compiled .au3 file):

if \$a = \$b and \$a = \$c and \$a = \$d Then
~to be implemented~
endif

if \$a = \$b then
if \$a = \$c then
if \$a = \$d then
~to be implemented~
endif
endif
endif
...so you tested and the results were what?

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

##### Share on other sites

Hmm...

Dim \$a=2, \$b=2, \$c=2, \$d=2

If (\$a=\$b)+(\$a=\$c)+(\$a=\$d) = 3 Then
tooltip(">>>   YES   <<<")
Endif

... nevermind.

An interesting solution, but I don't expect it to beat-the-clock.

##### Share on other sites

\$a=1
\$b=2
\$c=3
\$t=TimerInit()
For \$i=1 To 1000000
If \$a+\$b=3 And \$c-\$b=1 And \$a=\$b/2 Then
EndIf
Next
MsgBox(0 ,"" , TimerDiff(\$t))

\$t=TimerInit()
For \$i=1 To 1000000
If \$a+\$b=3 Then
If \$c-\$b=1 Then
If \$a=\$b/2 Then
EndIf
EndIf
EndIf
Next
MsgBox(0 ,"" , TimerDiff(\$t))

\$t=TimerInit()
For \$i=1 To 1000000
Select
Case \$a+\$b=3 And \$c-\$b=1 And \$a=\$b/2
EndSelect
Next
MsgBox(0 ,"" , TimerDiff(\$t))

so.. the nested way is the slowest about 1 sec longer than the long if

and the select is about the same as the big if

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

##### Share on other sites

should have used...

\$c-\$b=\$a

This is an interesting comment, all by itself....

so.. the nested way is the slowest about 1 sec longer than the long if

and the select is about the same as the big if tongue.gif

lol

8)

##### Share on other sites

I ran this code

\$a=1
\$b=2
\$c=3

\$t=TimerInit()
For \$i=1 To 10000000
If \$a = \$b And \$a = \$c Then
EndIf
Next
MsgBox(0 ,"" , TimerDiff(\$t))

\$t=TimerInit()
For \$i=1 To 10000000
If \$a = \$b Then
If \$a = \$c Then
EndIf
EndIf
Next
MsgBox(0 ,"" , TimerDiff(\$t))

first msgbox = 11804.2980322918

second msgbox = 10834.2162900592

I conclude if a = b and a = c most of the time, then one long if statement is faster, but, if a is not equal to b or c most of the time, then a nested is faster.

Edited by javas

##### Share on other sites

It would be preferable to take statistical results and reduce the loop to something around 1-30 loops and build an inner loop which will do the main code so some "jumps" won't reduce the overall result and you'll get 20 numbers and min and max and avg:

##### Share on other sites

Just having fun here...

It's obvious this doesn't stand a chance when one of the first couple pairs of numbers don't match, since it will continue to make all 4 compares regardless of the values tested, but...

I was surprisd to see this smokes (only) the "nested if" method when all 4 numbers are the same:

\$t=TimerInit()

For \$i=1 To 1000000

If (\$a=\$*(\$a=\$c)*(\$a=\$d) Then

EndIf

Next

MsgBox(0 ,"" , TimerDiff(\$t))

Edited by Spiff59

##### Share on other sites

This post explains a lot about speed and AutoIt

http://www.autoitscript.com/forum/index.php?showtopic=51604

## Create an account

Register a new account

×

• Wiki

• Back

• Git