How to optimize the speed of a script as much as possible.

Helpful script writing tricks and HowTo's
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to optimize the speed of a script as much as possible.

03 Dec 2019, 16:36

dude, just start a new help thread and go over ur issue there, code and everything
99% the root cause has nothing at all to do with optimizations, setbatchlines, cpu cores, percentages, multithreading
hellas
Posts: 1
Joined: 24 Jan 2020, 11:57

Re: How to optimize the speed of a script as much as possible.

24 Jan 2020, 17:27

Sam_ wrote:
29 Nov 2019, 15:34
mojobadshah wrote:
29 Nov 2019, 14:26
What is the specific mechanism I need to initiate through AHK to push CPU rate up to at least x2 or ~50% or even more than this ?
AHK is single threaded. Each instance of a running script will only ever use a single CPU core. You can SetBatchLines to -1 and change the process priority to above normal, but beyond that there isn't much you can do to force AHK to utilize more resources in order to run faster. If you haven't already, reading the Performance section of RegExReplace might be insightful.
Why not High process priority? why "Avobenormal" is the best option?
Sam_
Posts: 146
Joined: 20 Mar 2014, 20:24

Re: How to optimize the speed of a script as much as possible.

24 Jan 2020, 18:02

hellas wrote:
24 Jan 2020, 17:27
Sam_ wrote:
29 Nov 2019, 15:34
mojobadshah wrote:
29 Nov 2019, 14:26
What is the specific mechanism I need to initiate through AHK to push CPU rate up to at least x2 or ~50% or even more than this ?
AHK is single threaded. Each instance of a running script will only ever use a single CPU core. You can SetBatchLines to -1 and change the process priority to above normal, but beyond that there isn't much you can do to force AHK to utilize more resources in order to run faster. If you haven't already, reading the Performance section of RegExReplace might be insightful.
Why not High process priority? why "Avobenormal" is the best option?
SvenBent wrote:
26 Mar 2016, 14:34
May i put in a small warning against "Process, Priority, , H" especially for big CPU heavy scripts

Process, Priority, , H puts you priority in the group with keyboard input and network drivers. So any CPU heavy sections of the script is going to infuse lag on both KB and network.
However if you script does not have any CPU heavy.sections there is no real benefit from adjusting priory. may i instead suggest do do only "Process, Priority, , A" it will still keep you priority above any other running software but below network and keyboard. Since Windows priority model is blocking priority ( With a lot of exceptions) there is no difference from being in High vs Above, when all other software is in Normal anyway. ( aka multiple steps higher priority is just as good as one step higher)

in short: unless you have a very specific technically reason. use only "Process, Priority, , A" to increase script priority.
serg
Posts: 56
Joined: 21 Mar 2015, 05:33

Optimizing Loop

20 Mar 2021, 07:39

I was wondering how much "Loop" slows things down. So I tested it in comparison with simple math funcs. Maybe somebody will find this useful in optimizing their scripts.

First test, 30 additions of 1 var:

Code: Select all

t := A_TickCount
Loop 100000
 Loop 30
  a%a_index%++
t := A_TickCount - t

tt := A_TickCount
Loop 100000
 a1++ , a2++ , a3++ , a4++ , a5++ , a6++ , a7++ , a8++ , a9++ , a10++ , a11++ , a12++ , a13++ , a14++ , a15++ , a16++ , a17++ , a18++ , a19++ , a20++ , a21++ , a22++ , a23++ , a24++ , a25++ , a26++ , a27++ , a28++ , a29++ , a30++
tt := A_TickCount - tt
msgbox % "Time in milisec:`n" t "`n" tt "`n`nSpeed difference = " Round(t/tt,1)
Second test, 30 additions of 4 vars:

Code: Select all

t := A_TickCount
Loop 30000
 Loop 30
  a%a_index%++ , b%a_index%++ , c%a_index%++ , d%a_index%++
t := A_TickCount - t

tt := A_TickCount
Loop 30000
 a1++ , b1++ , c1++ , d1++ , a2++ , b2++ , c2++ , d2++ , a3++ , b3++ , c3++ , d3++ , a4++ , b4++ , c4++ , d4++ , a5++ , b5++ , c5++ , d5++ , a6++ , b6++ , c6++ , d6++ , a7++ , b7++ , c7++ , d7++ , a8++ , b8++ , c8++ , d8++ , a9++ , b9++ , c9++ , d9++ , a10++ , b10++ , c10++ , d10++ , a11++ , b11++ , c11++ , d11++ , a12++ , b12++ , c12++ , d12++ , a13++ , b13++ , c13++ , d13++ , a14++ , b14++ , c14++ , d14++ , a15++ , b15++ , c15++ , d15++ , a16++ , b16++ , c16++ , d16++ , a17++ , b17++ , c17++ , d17++ , a18++ , b18++ , c18++ , d18++ , a19++ , b19++ , c19++ , d19++ , a20++ , b20++ , c20++ , d20++ , a21++ , b21++ , c21++ , d21++ , a22++ , b22++ , c22++ , d22++ , a23++ , b23++ , c23++ , d23++ , a24++ , b24++ , c24++ , d24++ , a25++ , b25++ , c25++ , d25++ , a26++ , b26++ , c26++ , d26++ , a27++ , b27++ , c27++ , d27++ , a28++ , b28++ , c28++ , d28++ , a29++ , b29++ , c29++ , d29++ , a30++ , b30++ , c30++ , d30++
tt := A_TickCount - tt
msgbox % "Time in milisec:`n" t "`n" tt "`n`nSpeed difference = " Round(t/tt,1)
In first test, speed difference came to be ~3.5, in second ~5, which means that loop (inner one) + resolving var references (a%a_index%>a[1,2..]) consumed 3.5-5 times more time than actual calculations! Clearly this difference is getting less as complexity of calculation increases. Still, in some situations where Loop has only calculations inside without "if/break/continue...", and performance matters (as it does in my project), replacing Loop like in example above speeds things a lot.

Obviously, no need to rewrite things manually, in the example above I used this:

Code: Select all

q := ""
Loop 30
 q .= "a" a_index "++ , b" a_index "++ , c" a_index "++ , d" a_index "++ , "
clipboard := q
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: How to optimize the speed of a script as much as possible.

20 Mar 2021, 13:54

Performance: [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.
See: operators in expressions
serg
Posts: 56
Joined: 21 Mar 2015, 05:33

Re: How to optimize the speed of a script as much as possible.

22 Mar 2021, 06:12

Re @Xtra
You are right, part of speed improvement came from use of comma.
Same test as above but without commas:

Code: Select all

t := A_TickCount
Loop 100000
 Loop 30
  a%a_index%++
t := A_TickCount - t

tt := A_TickCount
Loop 100000 {
 a1++
 a2++
 a3++
 a4++
 a5++
 a6++
 a7++
 a8++
 a9++
 a10++
 a11++
 a12++
 a13++
 a14++
 a15++
 a16++
 a17++
 a18++
 a19++
 a20++
 a21++
 a22++
 a23++
 a24++
 a25++
 a26++
 a27++
 a28++
 a29++
 a30++
 }
tt := A_TickCount - tt
msgbox % "Time in milisec:`n" t "`n" tt "`n`nSpeed difference = " Round(t/tt,1)
without commas speed difference came down from ~5 to ~3
20170201225639
Posts: 144
Joined: 01 Feb 2017, 22:57

Re: How to optimize the speed of a script as much as possible.

23 Sep 2021, 12:43

> Gosub VS functions
> SvenBent wrote:
> This is probably way down into miniscule area but
> Using Gosub seems to be 25% faster than using a function.

I wonder if this is still true. I recently tested function call vs. gosub vs. method call wrt efficiency. Function call indisputably came out on top (I expected gosub to be faster)
Gildum
Posts: 1
Joined: 08 Nov 2021, 09:32
Contact:

Re: How to optimize the speed of a script as much as possible.

30 Nov 2021, 04:37

WAZAAAAA wrote:
14 Feb 2015, 14:00
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.
I just did a speed test with mousemove commands and SendInput actually does not ignore SetMouseDelay and SetDefaultMouseSpeed. So it's actually necessary to include SetMouseDelay -1
and SetDefaultMouseSpeed 0 for maximum script speed. I'm on Autohotkey v1.1.33.10.

Thanks for all the helpful optimization tips everyone!
Fidanza
Posts: 2
Joined: 10 Sep 2020, 09:59

Re: How to optimize the speed of a script as much as possible.

25 Dec 2021, 14:33

WAZAAAAA wrote:
14 Feb 2015, 14:00

DllCall("Sleep",UInt,17) ;I just used the precise sleep function to wait exactly 17 milliseconds[/code]
hey the precision of this script has gone after windows 10 1803.. :!: :problem: i mean with windows 1809 and later windows 10 now uses QueryPerformanceFrequency as a fixed 10MHZ value and because of this the precision and speed of this command vanished..and i really dont understand why nobody aware of this problem.. :roll: the same problem happening with steelseries keyboard macros..macros work but slower than before on windows 10 1809 and later.. anybody know this or is there a work around to fix this ? thnx
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: How to optimize the speed of a script as much as possible.

05 Feb 2022, 19:49

#1
a .= "c" vs this.a .= "c"
Spoiler

#2
VarSetCapacity(a, 2*iterations) vs none (auto-expanding VarSetCapacity(a, 0)) in concatenation a .= "c" test.
Spoiler

#3
a++ vs this.a++
Spoiler

#4
a++ vs this.Add1(a) test.
Add1(ByRef n) {
n++
}
Spoiler

#5
a++ vs not ByRef a := this.Add1(a) test.
Add1(n) {
return ++n
}
Spoiler

#6
a .= "c" vs ByRef this.ConcatenateC(b)
ConcatenateC(ByRef str) {
str .= "c"
}
Spoiler

#7
a .= "c" vs Not ByRefb := this.ConcatenateC(b)
ConcatenateC(str) {
return str .= "c"
}
Spoiler

#8
a .= "c" vs Not ByRef, Not inside a class b := ConcatenateC(b)
ConcatenateC(str) {
return str .= "c"
}
Spoiler

#9
a .= "c" vs ByRef, Not inside a class ConcatenateC(b)
ConcatenateC(ByRef str) {
str .= "c"
}
Spoiler
Last edited by amateur+ on 08 Feb 2022, 08:05, edited 4 times in total.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
ScripterBoi
Posts: 14
Joined: 17 Jan 2022, 11:31

Re: How to optimize the speed of a script as much as possible.

06 Feb 2022, 11:40

What is the Tick Count?(im dumb)
Ive noticed that SendPlay's Tick Count is the lowest one
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: How to optimize the speed of a script as much as possible.

06 Feb 2022, 13:27

@ScripterBoi, you can read about it here: TickCount.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
Sam_
Posts: 146
Joined: 20 Mar 2014, 20:24

Re: How to optimize the speed of a script as much as possible.

06 Feb 2022, 20:56

@amateur+
The more layers of obfuscation you add, the more processor operations and thus the slower AHK will be. Embedding "variables" within objects or classes adds layers of obfuscation. Manipulating a simple variable will ALWAYS be slower than performing an equivalent operation within an object or class. Calling a function directly will always be quicker than calling an equivalent method in a class. This shouldn't be surprising, and this won't change no matter what the operation is.
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: How to optimize the speed of a script as much as possible.

06 Feb 2022, 21:02

@Sam_, yes, but it is interesting to see some figures.
Last edited by amateur+ on 07 Feb 2022, 12:26, edited 1 time in total.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: How to optimize the speed of a script as much as possible.

06 Feb 2022, 21:26

amateur+ wrote:
06 Feb 2022, 21:02
@Sam_, yes, but it is interesting to see some figures.
its not really interesting at all, until you provide something that would benchmark opposite from what our common sense would tell us.

so far, we all expect that less complexity to be faster than more complexity, and thats all you've trivially shown

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: How to optimize the speed of a script as much as possible.

06 Feb 2022, 21:44

@guest3456, and you really expected not ByRef concatenation to be 100+ times less effective than ByRef one?
Also did you expected the result from #1 (here)?
I've just noticed that my timer subroutine inside a class set up for 100 ms updates far less frequently than every 100 ms and even 200 ms. I ran some tests to figure out what degree of influence some nuances have and decided to share these tests.
But I think I should gather them in one post instead of spamming. Maybe tomorrow I'll put them all in one post and delete the others.

EDIT: Done. Now moderators are encouraged to remove "cleared" posts since I'm not able to do that on my own.
[Mod edit: Done.]
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
User avatar
juxwillx
Posts: 3
Joined: 20 May 2021, 15:30

Re: How to optimize the speed of a script as much as possible.

16 Feb 2022, 01:55

You're a saviour. Thanks man!
User avatar
WAZAAAAA
Posts: 88
Joined: 13 Jan 2015, 19:48

Re: How to optimize the speed of a script as much as possible.

16 Aug 2022, 19:49

Fidanza wrote:
25 Dec 2021, 14:33
WAZAAAAA wrote:
14 Feb 2015, 14:00

DllCall("Sleep",UInt,17) ;I just used the precise sleep function to wait exactly 17 milliseconds[/code]
hey the precision of this script has gone after windows 10 1803.. :!: :problem: i mean with windows 1809 and later windows 10 now uses QueryPerformanceFrequency as a fixed 10MHZ value and because of this the precision and speed of this command vanished..and i really dont understand why nobody aware of this problem.. :roll: the same problem happening with steelseries keyboard macros..macros work but slower than before on windows 10 1809 and later.. anybody know this or is there a work around to fix this ? thnx
cleaned and fixed the first post here and there
but the most important change is the high precision Sleep code thoroughly explained in point 9. which involves changing the global Windows Resolution Timer... that should fix your Sleep timing issues
YOU'RE NOT ALEXANDER
Xendrus
Posts: 2
Joined: 30 Jul 2023, 08:58

Re: How to optimize the speed of a script as much as possible.

01 Aug 2023, 15:53

SetBatchLines, -1 Causes my games to completely freeze to 0 fps until the key is released.
User avatar
Animan8000
Posts: 56
Joined: 11 May 2022, 05:00
Contact:

Re: How to optimize the speed of a script as much as possible.

02 Aug 2023, 06:18

Xendrus wrote:
01 Aug 2023, 15:53
SetBatchLines, -1 Causes my games to completely freeze to 0 fps until the key is released.
That has nothing to do with AHK. Some games don't handle massive inputs at once, resulting into a freeze for some of them. Same story with Chromium and I think Electron based applications, if you spam too fast.

Return to “Tutorials (v1)”

Who is online

Users browsing this forum: No registered users and 33 guests