Jump to content

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


javas
 Share

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
Link to comment
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
Link to comment
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

Link to comment
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
Link to comment
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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...