Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

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


  • Please log in to reply
16 replies to this topic
WAZAAAAA
  • Members
  • 14 posts
  • Last active: Sep 30 2015 06:34 PM
  • Joined: 25 May 2013
This thread has moved to ahkscript.org
http://ahkscript.org....php?f=6&t=6413

autohotkey.com is deprecated. The download page, the online documentation and the forum boards are either outdated or broken and everyone is recommended to move there.

A v i
  • Members
  • 1323 posts
  • Last active: Nov 14 2015 06:56 PM
  • Joined: 30 Jan 2013

I've read that SendInput and SendPlay perform faster than the default SendEvent. Is this true? What's the fastest between Input and Play then? What if I'm using "SetKeyDelay, -1" with SendEvent?

Yes, Play is faster then Event but there is something which can be more faster and that is Windows clipboard.

You would have noticed how fast Ctrl+V copies the vast text from clipboard to any application.

To use Windows clipboard as an alternative for Send , you can search Clip() or make your own simple function like

;SendClip("this is text")
;return

SendClip(Data){
	oldclip := ClipboardAll , Clipboard := Data
	Send, ^v
	Clipboard := oldclip
}

I don't understand the difference between "SetMouseDelay, 0" and "SetMouseDelay, -1". Which one should I use for more speed/less delays?

Use -1

 

Is there a difference if the source of my script uses capital letters or not speed wise? Like, maybe AHK can interpret them faster if they are in some specific way. I usually type sources completely in lowercase

Not sure. shouldn't be

 

Does #NoEnv make a difference in speed?

It should. NoEnv disables Environment variables .

 

 I use MPRESS to encrypt some of my compiled scripts. I know for sure that the startup time of the program gets slowed down, but are the functions of my program being slowed down too?

No. I think the binaries are decompressed in memory and so there is start up lag. Non-autoexecute sections like functions should be normal.

 

 

 

Is there a difference between AHK L and AHK basic for these commands speed wise?
Is it bad if I'm using a 32bit script on a 64bit machine instead of a 64bit script on a 64bit machine speed wise?
Is there a difference between using an Unicode script and an ANSI script speed wise?

No/Minimal for me


Now a CS Undergrad. | My WebsiteAutohotkey Scripts | Softwares

Telegram me : @aviaryan


WAZAAAAA
  • Members
  • 14 posts
  • Last active: Sep 30 2015 06:34 PM
  • Joined: 25 May 2013

Yes, Play is faster then Event but there is something which can be more faster and that is Windows clipboard.
You would have noticed how fast Ctrl+V copies the vast text from clipboard to any application.
To use Windows clipboard as an alternative for Send , you can search Clip() or make your own simple function like

Thanks for the idea, but for what I need, not only real keyboard inputs must be simulated but also mouse clicks, and the clipboard is unusable.
 

No. I think the binaries are decompressed in memory and so there is start up lag. Non-autoexecute sections like functions should be normal.

I've asked this because I know an advanced EXE packer called Themida that can virtualize each single API call within the program to encrypt, and as a result it slows down all the functions of the program, but I doubt that the more basic EXE compression tools such as MPRESS and UPX affect this.

A v i
  • Members
  • 1323 posts
  • Last active: Nov 14 2015 06:56 PM
  • Joined: 30 Jan 2013

There is Click and MouseMove command for Mouse Clicks, use it with SetMouseDelay, -1.

That's the best you can get

 

 

TI've asked this because I know an advanced EXE packer called Themida that can virtualize each single API call within the program to encrypt, and as a result it slows down all the functions of the program, but I doubt that the more basic EXE compression tools such as MPRESS and UPX affect this.

They probably dont, for me. I use UPX all the time.


Now a CS Undergrad. | My WebsiteAutohotkey Scripts | Softwares

Telegram me : @aviaryan


WAZAAAAA
  • Members
  • 14 posts
  • Last active: Sep 30 2015 06:34 PM
  • Joined: 25 May 2013
Thanks again A v i, I have updated the first post according to some of my doubts being dispelled.

If anyone can help me for the others, I will be grateful!

Linear Spoon
  • Members
  • 842 posts
  • Last active: Sep 29 2015 03:56 AM
  • Joined: 29 Oct 2011

Is there a difference if the source of my script uses capital letters or not speed wise? Like, maybe AHK can interpret them faster if they are in some specific way. I usually type sources completely in lowercase.

No matter how you type it, Autohotkey will process variables, goto locations, etc into memory addresses before the script starts running. This won't take a noticeable amount of time.

 

As for the automation stuff (sending clicks/keys), I wouldn't worry about speed there. AutoHotkey can already send input faster than most applications can respond to it.

 

You should be using AutoHotkey for its ease of use. If you have a legit need to design an extremely fast application, you'll want to use a compiled rather than interpreted language.


Join us at the new forum - http://www.ahkscript.org/

 


mark76uk
  • Members
  • 16 posts
  • Last active: Jan 15 2014 05:40 AM
  • Joined: 01 Jun 2007

Hello.  A related question.  

 

Does anyone know if there's any small speed difference between the first two examples below?

 

My question refers specifically to the speed of the if line.  Are comparisons involving a 'hard coded' string faster or slower than comparisons with variables?

varInput := "Hello"
varPassword := "Hello"
if (varInput = varPassword)
   MsgBox Match!

and

varInput := "Hello"
if (varInput = "Hello")
   MsgBox Match!

I'm asking this because I wonder if there is a performance hit when running hundreds of lines of code written as below.  It's a style I use quite a lot:-

flagHappy := "WhateverHere" ;the values aren't used, they're just a unique identifier
flagSad := "AnythingHere"
varHowIAmFeeling := flagHappy
if (varHowIAmFeeling = flagHappy)
{
   Msgbox Woohoo!
}
else if (varHowIAmFeeling = flagSad)
{
   Msgbox Awwwww!
}

Thanks for reading,

Mark



RHCP
  • Members
  • 1228 posts
  • Last active: Apr 08 2017 06:17 PM
  • Joined: 29 May 2006
3. Using the Fast parameter of PixelSearch
PixelSearch, blablablabla, Fast

 

 

Using gdi pixel search is considerably faster than using the included PixelSearch function. There are some GDIP pixel search libraries available on the forum.



robertcollier4
  • Members
  • 141 posts
  • Last active: Jun 29 2017 12:42 PM
  • Joined: 11 Nov 2012

You can also turn off ListLines as mentioned here: Script Performance:

ListLines, Off

Also, SetTitleMatch to match only exactly might make those operations a bit faster? (not confirmed)

SetTitleMatchMode, 3 ;A window's title must exactly match WinTitle to be a match.

Also not confirmed, but for scripts that have a lot of stuff or a lot of timers perhaps could increase number of threads from default of 10 to 20.

#MaxThreads 20 ; increase from default of 10

Also, from http://www.autohotke...Performance.htm

To maximize performance, avoid using SetFormat


RHCP
  • Members
  • 1228 posts
  • Last active: Apr 08 2017 06:17 PM
  • Joined: 29 May 2006
#MaxThreads 20 ; increase from default of 10

Also, from http://www.autohotke...Performance.htm

 

This is a bit of a detour, but last year I increased this value (can't remember the number I used, but it wasn't anything stupidly high) in a very complex script of mine and I started getting all of these really weird errors. Like variables and clipboards would be overwritten or changed with hexidecimal values. I did have a heap of variables with hex values, but the ones being overwritten never should have had these values. As soon as I changed this (and only this) value back the problems instantly stopped. This is a pretty useless post with no evidence, but I originally did a search and didn't find anything relating to it..



robertcollier4
  • Members
  • 141 posts
  • Last active: Jun 29 2017 12:42 PM
  • Joined: 11 Nov 2012

Seems that using short variable names and short function names can yield a 5-20% performance boost when those variables/functions are used repeatedly such as in a Loop or array iteration.

 

What about an option in the AutoHotkey interpreter to replace all the variable and functions name in the script with short ones before loading a script? (obviously would make it hard to debug via MyListVars and ListLines - but most of the time these are not needed). I really like using long-descriptive-friendly variable and function names when coding, but don't want to have to take a performance hit for making my code easy to read.

 

Supporting data from AutoIt:

http://www.autoitscr...ze/#entry717285

TEST: Variable name "sVeryMuchLongerVariable" vs "sShort": 5% slower

 

http://www.autoitscr...e-2#entry729583

TEST: Function call "short()" vs "VeryMuchLongerFunctionName()": 16% slower



Linear Spoon
  • Members
  • 842 posts
  • Last active: Sep 29 2015 03:56 AM
  • Joined: 29 Oct 2011

No matter how you type it, Autohotkey will process variables, goto locations, etc into memory addresses before the script starts running. This won't take a noticeable amount of time.

 

@robertcollier4 AutoIt is not AutoHotkey. Test this yourself. There is not a significant difference between short and long variable names in AutoHotkey, because they're translated to memory addresses before the script starts.

start1 := A_TickCount
Loop, 10000000
  i := 1
end1 := A_TickCount

start2 := A_TickCount
Loop, 10000000
  i_am_a_very_long_variable_name := 1
end2 := A_TickCount

Msgbox % end1-start1 "`n" end2-start2

Note: dynamic variable references (using %% around a var) must be looked up at runtime - here you will see a performance hit for long variable names.

short := "i"
long := "i_am_a_very_long_variable_name"

start1 := A_TickCount
Loop, 10000000
  %short% := 1
end1 := A_TickCount

start2 := A_TickCount
Loop, 10000000
  %long% := 1
end2 := A_TickCount

Msgbox % end1-start1 "`n" end2-start2

Also note the same translation to memory addresses takes place for functions - again there is no significant difference between using long and short function names:

start1 := A_TickCount
Loop, 10000000
  i()
end1 := A_TickCount

start2 := A_TickCount
Loop, 10000000
  i_am_a_very_long_function_name()
end2 := A_TickCount

Msgbox % end1-start1 "`n" end2-start2

i_am_a_very_long_function_name()
{
  return 1
}

i()
{
  return 1
} 

Join us at the new forum - http://www.ahkscript.org/

 


guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011

You can also turn off ListLines as mentioned here: Script Performance:

ListLines, Off


learn something new every day! thanks

trismarck
  • Members
  • 390 posts
  • Last active: Nov 25 2015 12:35 PM
  • Joined: 02 Dec 2010

In addition to ListLines, there is also KeyHistory (if the script registers keypresses):

#KeyHistory 0

New Autohotkey forum: http://ahkscript.org.


gwarble
  • Members
  • 624 posts
  • Last active: Aug 12 2016 07:49 PM
  • Joined: 23 May 2009
Look up QueryPerformanceCounter (i think) so you can do your own tests and try different variations...
Loop your test by thousands and average to get better results... You're also getting into the realm where other running processes will have large effects on your tests, so watch out for that