Jump to content

ElseIf vs Else If


Recommended Posts

  • Moderators
2 hours ago, IAMK said:

Are they the same? Or is one faster than the other?

You understand you could have tested this yourself and had an answer in less time than it took to type out your post, right?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

3 hours ago, IAMK said:

Or is one faster than the other?

Speed is not a tenet of AutoIt.   Optimization time here is better spent sanitizing the input, as i dont know that you would even see measurable differences using a switch unless your dataset was huge.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

9 hours ago, IAMK said:

@JLogan3o13 Well, I was more looking for an answer, as I know some languages convert them to the same when compiled.

+ Some scenarios might be better/worse.

AutoIt is interpreted, not compiled. I suspect the first option will be quicker because there's less code, and less nesting. Practically it matters only if this is called repetitively in a very tight loop. From a tidiness point of view I don't see any gains in the second version, and actually see it as a messier solution.

$i = 0
$nul = ""
$hTimer = TimerInit()
For $i = 0 To 10000000
    If 1 = 0 Then
        ConsoleWrite("Math has changed" & @CRLF)
    ElseIf 1 = 1 Then
        $nul = ""
    EndIf
Next
ConsoleWrite("Elseif: " & TimerDiff($hTimer) & "ms"&@CRLF)

$hTimer = TimerInit()
For $i = 0 To 10000000
    If 1 = 0 Then
        ConsoleWrite("Math has changed" & @CRLF)
    Else
        If 1 = 1 Then
            $nul = ""
        EndIf
    EndIf
Next
ConsoleWrite("Else if: " & TimerDiff($hTimer) & "ms"&@CRLF)
Elseif: 6375.40588783317ms
Else if: 7675.14516364101ms
+>18:31:47 AutoIt3.exe ended.rc:0
+>18:31:47 AutoIt3Wrapper Finished.

As expected. Likewise I expect that the sooner in an If -Elseif-Else statement it evaluates true, the quicker the execution, with it practically only mattering in a tightly called loop. So the most frequent, but logically correct, option should be higher up.

Link to comment
Share on other sites

21 minutes ago, IAMK said:

@TurionAltec I see. If that's the case, should all my variable names be as short as possible?

Only in the tightest of algorithms that run iteratively for a long time should use short variable names, or possibly the variable in a for-next loop. I recommend following the Best coding practices for variable names, which will make maintaining your code significantly easier. Particularly if you go back and modify a program your wrote 6 months or a year previous. Using good variable names will make the code "self-documenting", reducing what you actually need to add as comments. 

When writing code, readability should be above performance, except for super high performance cases. But at the same time, given equal readability, higher performance should should be chosen (so for example, don't call @ComSpec unless you need an internal cmd.exe command) .

If you have code run in a tight loop such that the name makes such a performance difference, you should look at doing it in a compiled language where you can use sensible variable names during development, that will compile down to the same thing whether you use a long name or a short name.

 

$hTimer = TimerInit()
For $i = 0 To 10000000
    $x = ""
Next
ConsoleWrite("Short var name:" & TimerDiff($hTimer) & @CRLF)

$hTimer = TimerInit()
For $i = 0 To 10000000
    $Sensiblelengthvar = ""
Next
ConsoleWrite("Sensible var name:" & TimerDiff($hTimer) & @CRLF)


$hTimer = TimerInit()
For $i = 0 To 10000000
    $veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylongvar = ""
Next
ConsoleWrite("Very Long var name:" & TimerDiff($hTimer) & @CRLF)
Short var name:1698.24554406784
Sensible var name:1782.88926743569
Very Long var name:2066.1672312123
+>19:47:17 AutoIt3.exe ended.rc:0
+>19:47:17 AutoIt3Wrapper Finished.

 

Link to comment
Share on other sites

@TurionAltec Thank you for the good link.

I will continue developing my script as is. For the final version, as all modifications are handled by a text file, I can make a second copy of the script and find/replace all variables with v1, v2, v3, v4...

The difference is minuscule, so I wonder if I even need to bother to do the above, as I don't use for loops like that.

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...