Please note that inside the spoiler of note #8 you can find a good benchmark script to test the speed of your scripts.
Just copy this at the beginning of any AHK script to make use of the performance optimizations:
Code: Select all
;OPTIMIZATIONS START #NoEnv #MaxHotkeysPerInterval 99000000 #HotkeyInterval 99000000 #KeyHistory 0 ListLines Off Process, Priority, , A SetBatchLines, -1 SetKeyDelay, -1, -1 SetMouseDelay, -1 SetDefaultMouseSpeed, 0 SetWinDelay, -1 SetControlDelay, -1 SendMode Input DllCall("ntdll\ZwSetTimerResolution","Int",5000,"Int",1,"Int*",MyCurrentTimerResolution) ;setting the Windows Timer Resolution to 0.5ms, THIS IS A GLOBAL CHANGE ;OPTIMIZATIONS END ;YOUR SCRIPT GOES HERE DllCall("Sleep","UInt",1) ;I just slept exactly 1ms! DllCall("ntdll\ZwDelayExecution","Int",0,"Int64*",-5000) ;you can use this to sleep in increments of 0.5ms if you need even more granularity
1. #NoEnv is recommended for all scripts, it disables environment variables.
2. The default #MaxHotkeysPerInterval along with #HotkeyInterval will stop your script by showing message boxes if you have some kind of rapid autofire/turbo loop in it. Just put some insane unreachable high number to ignore this limit.
3. #KeyHistory and ListLines are functions used to "log your keys". Disable them as they're only useful for debugging purposes.
4. Setting a higher Priority to a Windows program is supposed to improve its performance. Use AboveNormal/A. If you feel like it's making things worse, comment or remove this line.
6. Even though SendInput ignores SetKeyDelay, SetMouseDelay and SetDefaultMouseSpeed, having these delays at -1 improves SendEvent's speed just in case SendInput is not available and falls back to SendEvent.
7. SetWinDelay and SetControlDelay may affect performance depending on the script.
8. SendInput is the fastest send method. SendEvent (the default one) is 2nd place, SendPlay a far 3rd place (it's the most compatible one though). SendInput does not obey to SetKeyDelay, SetMouseDelay, SetDefaultMouseSpeed; there is no delay between keystrokes in that mode.
11. According to the documentation, the Unicode x64bit version of AHK is faster, use it when available (this text is found in the setup file).
Previous inputs from users of the archived forum are located here: https://autohotkey.com/board/topic/94455-how-to-optimize-the-speed-of-a-script-as-much-as-possible/
For more optimizations read every page of this thread, users have performed many different tests!