Re: How to optimize the speed of a script as much as possible.
Posted: 23 Jun 2015, 19:23
by WAZAAAAA
Polished the first post quite a bit according to the replies.
-Added details to each command for the skeptic and some corrections.
-SendEvent replaced by SendInput as it seems that I was somehow wrong about Event's higher speed, I benchmarked these again recently and Input was faster like the documentation suggests. Testable benchmark script included into the first post.
-Removed #MaxThreads 255, #MaxMem 4095, #MaxThreadsBuffer On, since lexikos in person clarified that they don't provide any performance benefit. My bad for blindly including them into the code.
-Removed SetTitleMatchMode 2 and SetTitleMatchMode fast because it defaults to fast already and the MatchMode setting was arbitrary.
Re: How to optimize the speed of a script as much as possible.
Posted: 23 Jun 2015, 19:51
by lexikos
WAZAAAAA, your top post still contains the SetKeyDelay error.
Sam_ wrote:
lexikos wrote:
SetKeyDelay, -1, -1, -1
The third parameter should be the word "Play" or nothing at all. -1 has no meaning.
Thanks for the catch! I updated my last post.
"Nothing at all" (for the third parameter) is the more correct option in this case. If you use the word Play it will only affect SendPlay, which defaults to -1/-1 anyway.
Re: How to optimize the speed of a script as much as possible.
Posted: 23 Jun 2015, 20:43
by Joe Glines
Thanks for y'all working on this. I put something similar to the above in my library and then I call it at the beginning of all my scripts:
#Include <default_Settings>
This allows me to keep my script small yet have a lot of preferences selected. (I just like having less in my actual file )
Re: How to optimize the speed of a script as much as possible.
Posted: 24 Jun 2015, 09:41
by WAZAAAAA
lexikos wrote:WAZAAAAA, your top post still contains the SetKeyDelay error.
Sam_ wrote:
lexikos wrote:
SetKeyDelay, -1, -1, -1
The third parameter should be the word "Play" or nothing at all. -1 has no meaning.
Thanks for the catch! I updated my last post.
"Nothing at all" (for the third parameter) is the more correct option in this case. If you use the word Play it will only affect SendPlay, which defaults to -1/-1 anyway.
corrected, thanks for pointing it out again, and thanks for EXISTING
Re: How to optimize the speed of a script as much as possible.
Posted: 24 Jun 2015, 10:50
by Sam_
Is it okay to discuss programming techniques that will help "optimize the speed of a script as much as possible" in this topic? For instance, in my experience (and according to the documentation), using VarSetCapacity() improves performance when building a string by means of gradual concatenation. From the documentation:
In addition to its uses described at DllCall, this function can also be used to enhance performance when building a string by means of gradual concatenation. This is because multiple automatic resizings can be avoided when you have some idea of what the string's final length will be.
From the example in the documentation (although not a working example...):
; Optimize by ensuring MyVar has plenty of space to work with.
VarSetCapacity(MyVar, 10240000) ; ~10 MB
Loop
{
...
MyVar = %MyVar%%StringToConcatenate%
...
}
Re: How to optimize the speed of a script as much as possible.
Posted: 24 Jun 2015, 12:09
by WAZAAAAA
Sam_ wrote:Is it okay to discuss programming techniques that will help "optimize the speed of a script as much as possible" in this topic
Yes, as the first post mentions:
WAZAAAAA wrote:GOTTA GO FAST
Re: How to optimize the speed of a script as much as possible.
Posted: 24 Jun 2015, 23:31
by KuroiLight
Sam_ wrote:So, taking the above into account, following the recommendations in the documentation (generally a wise move), and rearranging a bit, we now have:
#NoEnv
Process, Priority, , H
SetBatchLines, -1
ListLines Off
#KeyHistory 0
SendMode Input
SetTitleMatchMode 2
SetTitleMatchMode Fast
SetKeyDelay, -1, -1, Play
SetMouseDelay, -1
SetWinDelay, 0 ; Changed to 0 upon recommendation of documentation
SetControlDelay, 0 ; Changed to 0 upon recommendation of documentation
SetDefaultMouseSpeed, 0
; YOUR_SCRIPT_HERE
Edited SetKeyDelay. See next post.
Looks good.
Re: How to optimize the speed of a script as much as possible.
EDIT: according to a tickcount test this is actually slower; but it has the upside of not having to worry about whether the library is already loaded or not.
EDIT: well seems I made a mistake in not testing these properly before-hand: in that all other ways I've tried are still slower than the built-in dllcall, makes sense actually. (guess Ill be removing some code); I still think one form or another might help with function calls to libraries not yet loaded though, stay clear of lookuptables/dictionaries as those seem to be too slow for ahk though. (strangely enough dynamic variable version was the fastest of the 3)
Re: How to optimize the speed of a script as much as possible.
Posted: 08 Jul 2015, 10:06
by dd900
Documentation wrote: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.
Re: How to optimize the speed of a script as much as possible.
Posted: 09 Jul 2015, 21:14
by RHCP
KuroiLight wrote:
EDIT: well seems I made a mistake in not testing these properly before-hand: in that all other ways I've tried are still slower than the built-in dllcall, makes sense actually. (guess Ill be removing some code); I still think one form or another might help with function calls to libraries not yet loaded though, stay clear of lookuptables/dictionaries as those seem to be too slow for ahk though. (strangely enough dynamic variable version was the
It's not too surprising that those DLLCalls are slower. Those particular function names are automatically resolved to an address when AHK starts. Better results might be obtained when the function does not reside in User32.dll, Kernel32.dll, ComCtl32.dll, or Gdi32.dll.
However, I use the GDIP ahk library and decided to try jNizM's idea.
Test:
All the gdiPlus names were replaced with an object that stored the relevant function address.
A custom drawing function was then looped 1000 times.
Average Time Per Loop:
String Function Name: 14.9 ms
Function Address: 15.5 ms
Considering the data which was being drawn, there were probably a few hundred gdiPlus calls per loop. I wasn't expecting much of a performance boost, but it actually ended up being slower.....
Re: How to optimize the speed of a script as much as possible.
Posted: 10 Jul 2015, 00:39
by jNizM
@RHCP
Can you show me your testscript?
Re: How to optimize the speed of a script as much as possible.
Posted: 09 Sep 2015, 17:03
by WAZAAAAA
Replaced the high CPU Sleep function with the "suspend" Sleep method which uses no considerable CPU and makes the code way shorter.
Here's the previous one, for historical reasons I guess:
sleepz(period := 1, Mode := "P")
{
static Frequency, MinSetResolution, PID
if (Mode = "P")
{
pBatchLines := A_BatchLines
SetBatchLines, -1
if !Frequency
DllCall("QueryPerformanceFrequency", "Int64*", Frequency)
DllCall("QueryPerformanceCounter", "Int64*", Start)
Finish := Start + ( Frequency * (period/1000))
loop
DllCall("QueryPerformanceCounter", "Int64*", Current)
until (Current >= Finish)
SetBatchLines, %pBatchLines%
}
return
}
sleepz(16.6, "P") ;I just used the CPU intensive precise sleep function to wait exactly 16,6 milliseconds!
Higher precision but higher usage ?
And may I ask.
What's the lowest delay of the function
Re: How to optimize the speed of a script as much as possible.
Posted: 17 Sep 2015, 21:19
by WAZAAAAA
dsewq1LYJ wrote:Higher precision but higher usage ?
And may I ask.
What's the lowest delay of the function
According to my benchmarks:
Sleep default method = low CPU, low precision
Sleep suspend method = low CPU, high precision, issues with SetTimer
Sleep precise method = high CPU, higher precision
But sometimes when using the precise method, it eats so much CPU that it actually ends up slowing everything down, this has happened to a friend of mine too with a weaker PC, that's why I have replaced it with the suspend mode.
You can read more about precise sleeps at RHCP's thread: http://www.autohotkey.com/board/topic/9 ... -function/
I don't understand what do you mean with lowest delay.
Re: How to optimize the speed of a script as much as possible.
Posted: 22 Sep 2015, 04:06
by dsewq1LYJ
WAZAAAAA wrote:
dsewq1LYJ wrote:Higher precision but higher usage ?
And may I ask.
What's the lowest delay of the function
According to my benchmarks:
Sleep default method = low CPU, low precision
Sleep suspend method = low CPU, high precision, issues with SetTimer
Sleep precise method = high CPU, higher precision
But sometimes when using the precise method, it eats so much CPU that it actually ends up slowing everything down, this has happened to a friend of mine too with a weaker PC, that's why I have replaced it with the suspend mode.
You can read more about precise sleeps at RHCP's thread: http://www.autohotkey.com/board/topic/9 ... -function/
I don't understand what do you mean with lowest delay.
The PixelSearch is faster than PixelGetColor.
but I got a Question about it.
The "PixelSearch" cant store the color right ?
What I mean...is follow code maybe the PixelSearch is pointless ?
<Sorry about my horrible English...I'll try my best...>
e.g.
PixelSearch,,,1,1,1,1,0xFFFFFF
if !errorlevel
foo()
PixelSearch,,,1,1,1,1,0xEEEEEE
if !errorlevel
bar()
PixelSearch,,,1,1,1,1,0xDDDDDD
if !errorlevel
foo()
Re: How to optimize the speed of a script as much as possible.
Posted: 24 Sep 2015, 08:10
by dsewq1LYJ
ozzii wrote:If I understand well something like this:
PixelSearch,,,1,1,1,1,0xFFFFFF
if !errorlevel
foo()
PixelSearch,,,1,1,1,1,0xEEEEEE
if !errorlevel
bar()
PixelSearch,,,1,1,1,1,0xDDDDDD
if !errorlevel
foo()
Nice..but I still confusing ... Why PixelSearch faster than PixelGetColor ???...