How to optimize the speed of a script as much as possible.
Re: How to optimize the speed of a script as much as possible.
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
99% the root cause has nothing at all to do with optimizations, setbatchlines, cpu cores, percentages, multithreading
Re: How to optimize the speed of a script as much as possible.
Why not High process priority? why "Avobenormal" is the best option?Sam_ wrote: ↑29 Nov 2019, 15:34AHK 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.mojobadshah wrote: ↑29 Nov 2019, 14:26What 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 ?
Re: How to optimize the speed of a script as much as possible.
hellas wrote: ↑24 Jan 2020, 17:27Why not High process priority? why "Avobenormal" is the best option?Sam_ wrote: ↑29 Nov 2019, 15:34AHK 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.mojobadshah wrote: ↑29 Nov 2019, 14:26What 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 ?
SvenBent wrote: ↑26 Mar 2016, 14:34May 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.
Optimizing Loop
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:
Second test, 30 additions of 4 vars:
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:
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)
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)
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
Re: How to optimize the speed of a script as much as possible.
See: operators in expressionsPerformance: [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.
Re: How to optimize the speed of a script as much as possible.
Re @Xtra
You are right, part of speed improvement came from use of comma.
Same test as above but without commas:
without commas speed difference came down from ~5 to ~3
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)
-
- Posts: 144
- Joined: 01 Feb 2017, 22:57
Re: How to optimize the speed of a script as much as possible.
> 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)
> 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)
Re: How to optimize the speed of a script as much as possible.
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!
Re: How to optimize the speed of a script as much as possible.
hey the precision of this script has gone after windows 10 1803.. 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.. 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
Re: How to optimize the speed of a script as much as possible.
#1
a .= "c" vs this.a .= "c"
#2
VarSetCapacity(a, 2*iterations) vs none (auto-expanding VarSetCapacity(a, 0)) in concatenation a .= "c" test.
#3
a++ vs this.a++
#4
a++ vs this.Add1(a) test.
Add1(ByRef n) {
n++
}
#5
a++ vs not ByRef a := this.Add1(a) test.
Add1(n) {
return ++n
}
#6
a .= "c" vs ByRef this.ConcatenateC(b)
ConcatenateC(ByRef str) {
str .= "c"
}
#7
a .= "c" vs Not ByRefb := this.ConcatenateC(b)
ConcatenateC(str) {
return str .= "c"
}
#8
a .= "c" vs Not ByRef, Not inside a class b := ConcatenateC(b)
ConcatenateC(str) {
return str .= "c"
}
#9
a .= "c" vs ByRef, Not inside a class ConcatenateC(b)
ConcatenateC(ByRef str) {
str .= "c"
}
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.
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.
-
- Posts: 14
- Joined: 17 Jan 2022, 11:31
Re: How to optimize the speed of a script as much as possible.
What is the Tick Count?(im dumb)
Ive noticed that SendPlay's Tick Count is the lowest one
Ive noticed that SendPlay's Tick Count is the lowest one
Re: How to optimize the speed of a script as much as possible.
@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.
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.
Re: How to optimize the speed of a script as much as possible.
@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.
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.
Re: How to optimize the speed of a script as much as possible.
@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.
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.
Re: How to optimize the speed of a script as much as possible.
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
Re: How to optimize the speed of a script as much as possible.
@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.]
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.
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.
Re: How to optimize the speed of a script as much as possible.
You're a saviour. Thanks man!
Re: How to optimize the speed of a script as much as possible.
cleaned and fixed the first post here and thereFidanza wrote: ↑25 Dec 2021, 14:33hey the precision of this script has gone after windows 10 1803.. 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.. 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
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
Re: How to optimize the speed of a script as much as possible.
SetBatchLines, -1 Causes my games to completely freeze to 0 fps until the key is released.
- Animan8000
- Posts: 58
- Joined: 11 May 2022, 05:00
- Contact:
Re: How to optimize the speed of a script as much as possible.
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.