How to Optimize your AHKv2 Code for Speed (revisiting)

Helpful script writing tricks and HowTo's
sashaatx
Posts: 341
Joined: 27 May 2021, 08:27
Contact:

How to Optimize your AHKv2 Code for Speed (revisiting)

06 Jan 2024, 17:50

I'm not going to try to rewrite the original post text,
a lot of it is great and should be read from the source:
viewtopic.php?f=7&t=6413

Many of the conventions such as #NoEnv and #SetBatchLines are defunct, but the rest is very valuable. I have restructured the tests for ahkv2. https://github.com/samfisherirl/Optimize-AHKv2-Code-for-Speed


I really hope to get feedback from individuals with more knowledge than myself for additional areas of focus. If you see something missing, out of place, or any input at all, please share for future reference. Because this is strictly code, I was torn between posting to tutorials or scripts, mods feel free to correct me.


## Boolean

Code: Select all

        #SingleInstance Force
        #Requires Autohotkey v2
        /*
        credits:
            original post https://www.autohotkey.com/boards/viewtopic.php?f=7&t=6413
            WAZAAAAA https://www.autohotkey.com/boards/viewtopic.php?f=7&t=6413
            jNizM  https://www.autohotkey.com/boards/memberlist.php?mode=viewprofile&u=75
            lexikos https://www.autohotkey.com/boards/viewtopic.php?f=7&t=6413

        ahkv1 notes:
            a few tips for IF checking of Boolean values
            Tested on a Core2Quad Q6600 system.


            if VariableName
            Seems to be the fastest way to check if a variable is True


            if VariableName = 0
            Is the fastest way to check if a variable is false however it does not take into account of the variable is not set, aka empty. The IF commands does not get activaged if the variable is not set/empty

            if VariableName <> 1
            is almost as fast and an empty variable is considere false ( aka the IF settings get activated) just like if it contained a 0

            if Not VariableName
            Seems to be slower than both of the two above

        ahkv2 results on i9 laptop (rounded, try yourself for granular results):
            test1 time: 0.038134300000000003
            test2 time: 0.038739900000000001
            test3 time: 0.026265299999999998
            test4 time: 0.071452199999999993
            test5 time: 0.1123638
        */
        ; =========================================================================================================
        x := false
        QPC(1)

        Loop 1000000
        {
            if not x
                continue
        }
        test1 := QPC(0), QPC(1)

        Loop 1000000
        {
            if !x
                continue
        }
        test2 := QPC(0), QPC(1)

        x := true

        Loop 1000000
        {
            if x
                continue
        }
        test3 := QPC(0), QPC(1)


        Loop 1000000
        {
            if x = true
                continue
        }
        test4 := QPC(0), QPC(1)

        Loop 1000000
        {
            if x = 1
                continue
        }
        test5 := QPC(0)

        MsgBox("test1 time: "  test1 "`n" "test2 time: " test2 "`n" "test3 time: " test3 "`n" "test4 time: " test4 "`ntest5 time: " test5)
        FileAppend("`n========================" A_ScriptName "========================`n"
            "test1 time: "  test1 "`n" "test2 time: " test2 "`n" "test3 time: " test3 "`n" "test4 time: " test4 "`ntest5 time: " test5 "`n", "Log.txt")
        ExitApp()

        ; =========================================================================================================

        QPC(R := 0)
        {
            static P := 0, F := 0, Q := DllCall("QueryPerformanceFrequency", "Int64P", &F)
            return ! DllCall("QueryPerformanceCounter", "Int64P", &Q) + (R ? (P := Q) / F : (Q - P) / F) 
        }
## Math

Code: Select all

		/*
		credits:
    		original post https://www.autohotkey.com/boards/viewtopic.php?f=7&t=6413
    		WAZAAAAA https://www.autohotkey.com/boards/viewtopic.php?f=7&t=6413
    		jNizM  https://www.autohotkey.com/boards/memberlist.php?mode=viewprofile&u=75
    		lexikos https://www.autohotkey.com/boards/viewtopic.php?f=7&t=6413

		ahkv1 notes:
    		Avoid spreading math over multiple lines and using variable for intermediate results if they are only used once.
    		As much as possible condense math into one line and only use variables for storing math results if you need the results being used multiple times later.

    		remember that:
    		1x calc < 1x calc + 1x memory read < 2x calc

		ahkv2 results on i9 laptop (rounded, try yourself for granular results):
    		- result1: 1422
    		- result2: 1031
		*/

		#SingleInstance Force
		#Requires Autohotkey v2.0
		SendMode("Input")  ; Recommended for new scripts due to its superior speed and reliability.
		SetWorkingDir(A_ScriptDir)  ; Ensures a consistent starting directory.

		; REMOVED: SetBatchlines, -1
		ListLines(false)
		KeyHistory(0)

		var_Input1:=123
		var_Input2:=456


		start:=A_tickcount
		Loop 9999999
			{
			X:= (2 * var_Input1 ) -1
			Y:= (3 / var_Input2 ) +7
			Z:= X / Y
			}
		Results1:=A_tickcount - start


		start:=A_tickcount
		Loop 9999999
			{
			Z:= ((2 * var_Input1 ) -1) / ((3 / var_Input2 ) +7)
			}
		Results2:= A_tickcount - start


		MsgBox("result1: " Results1 "`nresult2: " Results2)
		FileAppend("`n========================" A_ScriptName "========================`n"
			"result1: " Results1 "`nresult2: " Results2 "`n", "Log.txt")
## Terinary

Code: Select all

       #SingleInstance Force
        #Requires Autohotkey v2.0
        /*
        credits:
            original post https://www.autohotkey.com/boards/viewtopic.php?f=7&t=6413
            WAZAAAAA https://www.autohotkey.com/boards/viewtopic.php?f=7&t=6413
            jNizM  https://www.autohotkey.com/boards/memberlist.php?mode=viewprofile&u=75
            lexikos https://www.autohotkey.com/boards/viewtopic.php?f=7&t=6413

        ahkv1 notes:
            Ternarry:        2.828439
            if/else:         3.931492

        ahkv2 results on i9 laptop (rounded, try yourself for granular results):
            - result1: 1.171
            - result2: 1.112
        */
        global lcnt := 10000000
        global VarA := "Hello"
        global VarT_1 := VarT_2 := False
        global VarI_1 := VarI_2 := False

        ; ===============================================================================================================================

        QPC(1)

        Loop lcnt
        {
            VarT_1 := (VarA = "Hello") ? True : False
            VarT_2 := (VarA = "World") ? True : False
        }
        test1 := QPC(0)

        ; ===================================================================================

        QPC(1)

        Loop lcnt
        {
            if (VarA = "Hello")
                VarI_1 := True
            else
                VarI_1 := False

            if (VarA = "World")
                VarI_2 := True
            else
                VarI_2 := False
        }
        test2 := QPC(0)

        ; ===================================================================================
        MsgBox("test1: " test1 "`ntest2: " test2)
        FileAppend("`n========================" A_ScriptName "========================`n"
            "test1: " test1 "`ntest2: " test2 "`n", "Log.txt")

        ExitApp()

        ; ===============================================================================================================================

        QPC(R := 0)
        {
            static P := 0, F := 0, Q := DllCall("QueryPerformanceFrequency", "Int64P", &F)
            return !DllCall("QueryPerformanceCounter", "Int64P", &Q) + (R ? (P := Q) / F : (Q - P) / F) 
        }
## Variable of similar value

Code: Select all

       #SingleInstance Force
        #Requires Autohotkey v2
        /*
        credits:
            original post https://www.autohotkey.com/boards/viewtopic.php?f=7&t=6413
            WAZAAAAA https://www.autohotkey.com/boards/viewtopic.php?f=7&t=6413
            jNizM  https://www.autohotkey.com/boards/memberlist.php?mode=viewprofile&u=75
            lexikos https://www.autohotkey.com/boards/viewtopic.php?f=7&t=6413

        ahkv1 notes:
            Performance: In v1.0.48+, the comma operator is usually faster than writing
            separate expressions, especially when assigning one variable to another
            (e.g. x:=y, a:=b). Performance continues to improve as more and more
            expressions are combined into a single expression; for example, it may be
            35% faster to combine five or ten simple expressions into a single expression.

        ahkv2 results on i9 laptop (rounded, try yourself for granular results):
            - test1: 0.09
            - test2: 0.11
            - test3: 0.15
            - test4: 0.31
        */
        ; =========================================================================================================

        QPC(1)

        Loop 1000000
        {
            t1a := 1
            t1b := 1
            t1c := 1
            t1d := 1
            t1e := 1
            t1f := 1
            t1g := 1
            t1h := 1
            t1i := 1
            t1j := 1
        }
        test1 := QPC(0), QPC(1)

        Loop 1000000
            t2a := t2b := t2c := t2d := t2e := t2f := t2g := t2h := t2i := t2j := 1
        test2 := QPC(0), QPC(1)

        Loop 1000000
            t3a := 1, t3b := 1, t3c := 1, t3d := 1, t3e := 1, t3f := 1, t3g := 1, t3h := 1, t3i := 1, t3j := 1
        test3 := QPC(0), QPC(1)


        Loop 1000000
            t3a := 1
            ,t3b := 1
            ,t3c := 1
            ,t3d := 1
            ,t3e := 1
            ,t3f := 1
            ,t3g := 1
            ,t3h := 1
            ,t3i := 1
            ,t3j := 1
        test4 := QPC(0)

        MsgBox("test1 time: "  test1 "`n" "test2 time: " test2 "`n" "test3 time: " test3 "`n" "test4 time: " test4)
        FileAppend("`n========================" A_ScriptName "========================`n"
            "test1 time: "  test1 "`n" "test2 time: " test2 "`n" "test3 time: " test3 "`n" "test4 time: " test4 "`n", "Log.txt")
        ExitApp()

        ; =========================================================================================================

        QPC(R := 0)
        {
            static P := 0, F := 0, Q := DllCall("QueryPerformanceFrequency", "Int64P", &F)
            return ! DllCall("QueryPerformanceCounter", "Int64P", &Q) + (R ? (P := Q) / F : (Q - P) / F) 
        }
## Variables of different values

Code: Select all

        #SingleInstance Force
        #Requires Autohotkey v2
        /*
        credits:
        original post https://www.autohotkey.com/boards/viewtopic.php?f=7&t=6413
        WAZAAAAA https://www.autohotkey.com/boards/viewtopic.php?f=7&t=6413
        jNizM  https://www.autohotkey.com/boards/memberlist.php?mode=viewprofile&u=75
        lexikos https://www.autohotkey.com/boards/viewtopic.php?f=7&t=6413
        
        testing variables of separate values on one line vs separate lines vs comma separated lines
        
        ahkv2 results on i9 laptop (rounded, try yourself for granular results):
        - test1: 0.119
        - test2: 0.223
        - test3: 0.468
        */
        ; =========================================================================================================

        QPC(1)

        Loop 1000000
        {
            t1a := 1
            t1b := 2
            t1c := 3
            t1d := 4
            t1e := 5
            t1f := 6
            t1g := 7
            t1h := 8
            t1i := 9
            t1j := 0
        }
        test1 := QPC(0), QPC(1)

        Loop 1000000
        {
            t3a := 1,t3b := 2,t3c := 3,t3d := 4,t3e := 5,t3f := 6,t3g := 7,t3h := 8,t3i := 9,t3j := 0
        }
        test2 := QPC(0), QPC(1)


        Loop 1000000
        {
            t3a := 1
            ,t3b := 2
            ,t3c := 3
            ,t3d := 4
            ,t3e := 5
            ,t3f := 6
            ,t3g := 7
            ,t3h := 8
            ,t3i := 9
            ,t3j := 0
            
        }
        test3 := QPC(0)

        MsgBox("test1 time: "  test1 "`n"  "test2 time: " test2 "`n" "test3 time: " test3)
        FileAppend("`n========================" A_ScriptName "========================`n"
            "test1 time: "  test1 "`n"  "test2 time: " test2 "`n" "test3 time: " test3 "`n", "Log.txt")
        ExitApp()

        ; =========================================================================================================

        QPC(R := 0)
        {
            static P := 0, F := 0, Q := DllCall("QueryPerformanceFrequency", "Int64P", &F)
            return ! DllCall("QueryPerformanceCounter", "Int64P", &Q) + (R ? (P := Q) / F : (Q - P) / F) 
        }
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

Re: How to Optimize your AHKv2 Code for Speed (revisiting)

21 Jan 2024, 00:57

So surprised! comma fasting the ahk script is not true.🤣
sashaatx
Posts: 341
Joined: 27 May 2021, 08:27
Contact:

Re: How to Optimize your AHKv2 Code for Speed (revisiting)

21 Jan 2024, 01:03

V2User wrote:
21 Jan 2024, 00:57
So surprised! comma fasting the ahk script is not true.🤣
it was in v1, and still can be with functions
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

Return to “Tutorials (v2)”

Who is online

Users browsing this forum: No registered users and 1 guest