Goal
Ok, I will try explaining the reason why I want to test this:
The reason why I'm testing this is because I want to be able to use different versions of functions. I added the version I want to use as the first parameter. I did it like this.
Code: Select all
; ImageSearchFunction (ISF) version definition
version:="v2"
; ISF call
ImageSearchFun(version)
; ISF function
ImageSearchFun(version:="basic", x1:=0, y1:=0, x2:=0, y2:=0, ImageFile:="S:\Path\Image.png"){
if (version="basic"){
ImageSearch, FoundX, FoundY, x1, y1, x2, y2, %ImageFile%
Msgbox basic stuff
}
else if (version="v1"){
ImageSearch, FoundX, FoundY, x1, y1, x2, y2, %ImageFile%
Msgbox v1 stuff
}
else if (version="v2"){
ImageSearch, FoundX, FoundY, x1, y1, x2, y2, %ImageFile%
Msgbox v2 stuff
}
}
Because this will result in a lot of else-if statements I wanted to test if this will slow down the runtime.
Different use of if statement within function speed (x=1)
I've created 5 situations (functions). I predefined x=1 and with the 5 functions I tested, I got the following results. Functions are shown below.
Code: Select all
; function 1
Speed1()
NextCol=0
Loop 1000000 ; loop 1.000.000 times
{
y=1 ; assign value
}
Speed2x()
; function 2
Speed1()
NextCol=0
Loop 1000000 ; loop 1.000.000 times
{
If (x=1) ; if x has a value of 1
y=2 ; assign value
}
Speed2x()
; function 3
Speed1()
NextCol=0
Loop 1000000 ; loop 1.000.000 times
{
If (x=0){ ; if x has a value of 1
y=3 ; assign value
}
else if (x=1) ; else if x has a value of 2
y=3 ; assign variable
else if (x=2)
y=3
else if (x=3)
y=3
else if (x=4)
y=3
}
Speed2x()
; function 4
Speed1()
NextCol=0
Loop 1000000 ; loop 1.000.000 times
{
If (x=0){ ; if x has a value of 0
y=4 ; assign value
}
else if (x=1){ ; else if x has a value of 1
if (x!=5){ ; if x doesn't have a value of 5
if (z=2){ ; if z has a value of 2
y=4 ; assign value
}}}
else if (x=2){
if (x!=5){
if (z=2){
y=4
}}}
else if (x=3){
if (x!=5){
if (z=2){
y=4
}}}
else if (x=4){
if (x!=5){
if (z=2){
y=4
}}}
}
Speed2x()
; function 5
Speed1()
NextCol=1
Loop 1000000 ; loop 1.000.000 times
{
If (x=0) ; if x has a value of 0
y=5 ; assign value
If (x=1) ; if x has a value of 1
y=5 ; assign value
If (x=2)
y=5
If (x=3)
y=5
If (x=4)
y=5
}
Speed2x()
- Function 1: 67,45ms
- Function 2: 175,88ms
- Function 3: 297,94ms
- Function 4: 603,92ms
- Function 5: 597,04ms
Are untrue statements read
Based on this I did some more testing. I wanted to know if untrue else-if statements were read and thus slow down runtime. To check this I predefined x=4 and ran function 3 and 4 again. Results are below
- Function 3: 606,97ms
- Function 4: 605,34ms
So my conclusion is that based on my testing, untrue if statements aren't read and will not slow down runtime.
Influence of else-if statements
Finally, I wanted to know if multiple else-if statements will effect the runtime. I ran function 3, 4 and 5 with x=1, x=2, x=3 and x=4.
- AHKForumTest.png (9 KiB) Viewed 3044 times
Results are that AHK slows down about 120ms (depending on size of content of else-if statement) per else-if statement (per 1.000.000 runs). Fun thing is that function 5 which uses separate if statements is faster when the amount of else-if statements is increased so maybe that is the way to go for me.
Response:
Benchmarking trivial code, such as simple conditional statements or assignments, is often futile, as execution takes so little time that the effects of those other factors can be significant by comparison. I would suggest benchmarking only when a performance issue actually exists, and to avoid making generalizations based on the results.
You are completely right. But instead of testing, I want to see if I could track down if the runtime will increase with the use of multiple (else-) if statements. I don't have a change of testing this on my main script yet and therefore wanted to try and find on the influence of (else-) if statements on the runtime. At least, try to get a somewhat reliable result.
Also, I note that SetBatchLines -1 was used nowhere in this thread.
I indeed didn't mention it but I'm using
SetBatchLines -1
Thanks for the info
@lexikos I appreciated your feedback.