Timing Script Speed (AutoHotkey Quick Tip)

Certain Types of Subroutines Tend to Eat Up Time (Loops, On Screen Changes, Multiple Drive Accesses, etc.)—Use This Simple Timer Routine to Figure Out How to Increase AutoHotkey Script Speed

Anytime you use AutoHotkey to make iterative changes in the controls in a GUI (Graphical User Interface) pop-up window, force multiple access to hard drive files, or implement repetitious subroutines (almost always with some form of a loop), you run the risk of slowing down your scripts. Minor changes to your script can make a significant difference in how fast it runs.

Library BenefitsLast time, I discussed the problem of slow data loading speeds when adding a large Hotstring file to the InstantHotstring.ahk app. I selected the EmojiInsert.ahk file (1036 Hotstrings) as a test file and watched as AutoHotkey carefully render each flashing Hotstring in the GUI window. I have since implemented techniques for hiding the GUI window and adding a Progress GUI control, but in the process, I needed a technique for measuring and comparing the load times for the various setups. (In the next blog, I plan to review the drag reducing steps I took to slash Hotstring loading time.) As a timer, I implemented the method found in the AutoHotkey online documentation for measuring the elapsed time in an AutoHotkey script:

StartTime := A_TickCount
;
; insert target AutoHotkey code here
;
ElapsedTime := A_TickCount - StartTime
MsgBox,  %ElapsedTime% milliseconds have elapsed.

To test execution speed, surround snippets of code with these three lines (one before and two after) at any suspected problem spot within a slow-running script.

Three Steps to Implementing the Script Speed Timer Subroutine

I could have used my watch to time the script, but this code gives accuracy while eliminating human error. Take the following steps to add a script timer to any section of code by enclosing it as follows:

  1. At the beginning of the code snippet you want timed, set the start time by storing the system variable A_TickCount to the variable StartTime. The variable A_TickCount stores the “number of milliseconds since the computer was rebooted.”
  2. At the endpoint of the target subroutine, calculate the time interval (ElapseTime) by subtracting StartTime from the current value of A_TickCount.
  3. Display the results in a MsgBox.

I surrounded the key portion of the LoadHotstrings subroutine starting at the point where AutoHotkey begins loading the opened Hotstring file and ending after completing the load loop. The code opens with:

HotstringCount := 0
StrReplace(AddHotstring, "`n" , "`n", OutputVarCount)
Gui, Hotstring:Show,Hide
StartTime := A_TickCount

Progress, R0-%OutputVarCount%,%OutputVarCount% lines in file
        ,Loading Hotstrings • • • ,%OpenFile%

This code counts the number of lines in the file with the StrReplace() function by finding and replacing the newline character (`n), hides the GUI window using Gui, Hotstring:Show, Hide, saves the time starting point StartTime, and initiates the Progress GUI control.

Inside the Hotstring loading loop:

HotstringCount++

Increments HotstringCount for each new Hotstring loaded from the file and:

Progress, %A_Index% ,%A_Index% of %OutputVarCount% lines in file

Updates the Progress GUI control with the current line count (A_Index).

Upon loop completion:

Progress, Off
Gui, Hotstring:Show, , Instant Hotstrings
ElapsedTime :=format("{1:0.3f}" ,(A_TickCount - StartTime)/1000)
MsgBox, %HotstringCount% Hotstrings Loaded!`r%ElapsedTime% Seconds!

AutoHotkey turns off the Progress GUI control, displays the GUI window again, calculates the total elapsed time, then displays the number of Hotstrings and the resulting runtime in a MgsBox pop-up.

This image displays the final result showing a load time of 40 seconds:

InstantHotstring Load Time
The initial timelapse for my first test of the LoadHotstrings subroutine returned a 40-second interval.

By temporarily removing key lines of code by placing the semicolon (;) comment character in front of each, I tested various configurations of the script. The key components I tested included:

  1. Hiding the GUI window while loading the Hotstrings. This saved the time it takes for AutoHotkey to render the GUI controls while loading each Hotstring.
  2. Running the LoadHotstrings subroutine both with and without a Progress GUI control.
  3. Running the Progress GUI control both with and without an incrementing count.
  4. Loading the InsertEmojis.ahk file twice. The first time loads a clean set of Hotstrings. The second addition loads duplicates.

Tip: The delete duplicates Hotstrings routine does not work on hidden windows since the ControlGet and Control commands don’t see hidden windows. Only the original configuration with the GUI pop-up visible prevents the addition of dups.

A number of factors affect script running times, therefore times will vary—although they should come close depending upon the computer and other processes running on the computer:

Technique File First Load File Reload
Original Configuration Showing GUI Window and the Counting Progress Bar 39.969 Seconds 74.516 Seconds
Hides the GUI Window 7.172 Seconds 29.579 Seconds
Hides the GUI Window and Removes the Hotstring Count from Progress Bar 6.953 Seconds 25.875 Seconds
Hidden Window and No Progress Bar 5.641 Seconds 4.453 Seconds

Next time, I plan to detail the effect of each change and the tradeoffs I made in the InstantHotstring.ahk script, as well as, some important learning points.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)

jack

This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)

3 thoughts on “Timing Script Speed (AutoHotkey Quick Tip)

  1. […] In the blog, Use the FileSelectFile Command to Save Instant Hotstrings to an AutoHotkey File, I discussed how the InstantHotstring.ahk script saves a set of newly created Hotstrings to a data file. The flip side of the coin reads those same (or any other) Hotstring data files into the InstantHotstring.ahk script, (Loading Hotstrings into the InstantHotstring.ahk Script from Any AutoHotkey (.ahk) File). The solution to reading Hotstring files introduced the problem of the long load times for large files (Timing Script Speed). […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s