How to genuinely compile a script? Is there any "loop" faster alternative?

Get help with using AutoHotkey and its commands and hotkeys
User
Posts: 407
Joined: 26 Jun 2017, 08:12

How to genuinely compile a script? Is there any "loop" faster alternative?

13 Nov 2017, 17:01

I already found out that "loop" is slow in Interpreted languages (like AutoHotKey is) because at every loop iteration they must interpret\translate the loop's body script code into native machine instructions in order to allow the processor to understand and execute it!

Native\Compiled programs are faster than Interpreted languages, because they natively contain machine instructions, so, at every loop iteration there is no need to interpret\translate the loop's body script code because it is already in native machine instructions!

Ok, so the solution is to compile the AutoHotKey scripts and loop becomes fast, right? Wrong!

AutoHotKey compiled ".exe" files are not truly native\compiled programs! Here is the supposedly reason!

AHK "compiler" simply packages into the ".exe" files the AHK interpreter along with your source code (which is the script code and not the native machine instructions). So, when the ".exe" file is executed, AHK interpreter will still at every loop iteration interpret\translate the loop's body script code into native machine instructions in order to allow the processor to understand and execute it!

I don't know if the above is correct or wrong, but anyway, the question is, is there any "loop" faster alternative for AutoHotKey?
Last edited by User on 17 Nov 2017, 18:49, edited 1 time in total.
User avatar
jeeswg
Posts: 6655
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Is there any "loop" faster alternative?

13 Nov 2017, 17:58

So, if you hardcode the contents of the loop multiple times, and execute that, does that make it faster?

For specific tasks, there may be alternatives e.g. machine code functions, sometimes RegEx (slow) can be faster than using loops. So it would help if you specified the task. Cheers.

[EDIT:] If anyone knows, I would welcome any further details re. how Loop works, why it's slow. Thanks.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Is there any "loop" faster alternative?

13 Nov 2017, 19:01

jeeswg wrote:.
I can't give you any answers here because I'm not a programmer and I don't understand these kind of things!

I read somewhere that some scripting languages like PHP and JavaScript pre-compile the scripts source code before executing them!

So, it would mean that the native machine instructions interpreted\translated from the script code are executed directly, which means that, no interpretation\translation of the loop's body script code is necessary at every loop iteration!
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Is there any "loop" faster alternative?

17 Nov 2017, 18:35

jeeswg wrote:.
@jeeswg, I think this link might be of your interest!!!

It mentions that AutoHotkey classic, AutoHotkey_L, AutoHotkey v2, AutoHotkey_H etc compiled scripts are not genuinely compiled and why they are not faster than uncompiled scripts!

In the other hand, it mentions that IronAHK compiled scripts are genuinely compiled and that they are faster than uncompiled scripts!
User avatar
jeeswg
Posts: 6655
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to genuinely compile a script? Is there any "loop" faster alternative?

17 Nov 2017, 19:01

- Interesting link. I've now started a txt file collecting links on AutoHotkey 'behind the scenes', which I might post when I get a few links, say ten. At some point I intend to go through old AHK forum posts, and collect any relevant links.
- It seems that you've answered the first question in your thread title. Answer: you can't.
- I've never felt the need to make any of my scripts faster, other than a few examples where machine code or rethinking the script worked.
- Btw if you do have some specific thing you'd like to speed up, do mention it, because there may be ways to make it faster.
- Btw earlier, I had meant that you should try some benchmark tests. Create a script with the same bits of code repeated n times, versus a script that uses Loop, and compare the speed.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Capn Odin
Posts: 1303
Joined: 23 Feb 2016, 19:45
Location: Denmark

Re: How to genuinely compile a script? Is there any "loop" faster alternative?

17 Nov 2017, 19:16

You definitely can compile AHK since the script is being turned into instructions that are run on the cpu, if you store the instructions you would have a compiled AHK script. However this will only work for a script that terminates.

But I would suggest using another language for applications where the speed is very important or running code from another language from AHK.
Please excuse my spelling I am dyslexic.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: How to genuinely compile a script? Is there any "loop" faster alternative?

17 Nov 2017, 20:11

jeeswg wrote:.
@jeeswg, I don't need to speed up anything!

I just noticed for multiple times that loop is a little bit or very very slow when it must deal with millions iterations and I just wanted to know if there is any faster alternative or not!

It just happens to be that, AutoHotKey is an interpreted language that does not precompile its scripts before executing them! That's why loop is slow! And to make things worse, even the compiled scripts are slow because they are not genuinely compiled!
User avatar
Masonjar13
Posts: 1473
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: How to genuinely compile a script? Is there any "loop" faster alternative?

17 Nov 2017, 21:17

Define "slow?" I've tested with indefinite loops, and they were extremely consistent between iterations, at an average 49ms. (That hefty time is due to a DLL function that uses the filesystem.) In fact, the more iterations I introduce, the quicker it becomes. The first iteration typically takes the longest.

I'm unclear of the point you're trying to make. AHK has always been an interpreted language, that's no secret. And yes, compiled scripts are simply the interpreter with the script appended to it.

AutoHotkey is considered a third-tier language, which is a language that's dependent on two other languages. Those would be C/C++ (as one language in this case) and Assembly. This is similar to virtually every web-based language, such as JavaScript, CSS, HTML, etc. as they're all interpreted by the browser engine. However, if a browser was not written in native code, such as Java, they would then be fourth-tier languages, because Java requires an interpreter as well. If you've ever used an Android phone, you can make a safe assumption that all your apps are being interpreted via Java.

Of course, interpreted languages can only be as fast as it's interpreter. While AHK could be more efficient, it's still quite efficient. Seeing as you can use DLL's and machine code (search MCode on the forums) directly, it can be optimized nearly 1:1 with pure C/C++ (not say ALL scripts could be, but such scripts could be made).

You can test with a small benchmark script I wrote.

Could it be the case that you're overloading your CPU buffer? By that, I mean do you have something happening in the loop, but the CPU is unable to process it at optimal speed due to an overflow of requests to the CPU?
OS: Windows 7 Ultimate | Editor: Notepad++
My Personal Function Library | Check Out My Computer Rig
User avatar
KuroiLight
Posts: 327
Joined: 12 Apr 2015, 20:24
GitHub: KuroiLight

Re: How to genuinely compile a script? Is there any "loop" faster alternative?

17 Nov 2017, 21:33

Maybe I'm missing something, not entirely sure I understand the point of this thread?
You are asking an implementation specific question but then state:
User wrote:I can't give you any answers here because I'm not a programmer and I don't understand these kind of things!
The question you are asking is dependent on the problem/code suggested solutions may be regex, async file access or even offloading a specific loop to native code (via a native dll/exe).
User wrote:AHK "compiler" simply packages into the ".exe" files the AHK interpreter along with your source code
Unless this changed recently, This is correct.
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
[/color]
Prof Williams

Re: How to genuinely compile a script? Is there any "loop" faster alternative?

18 Nov 2017, 13:29

User wrote:I already found out that "loop" is slow in Interpreted languages (like AutoHotKey is) because at every loop iteration they must interpret\translate the loop's body script code into native machine instructions in order to allow the processor to understand and execute it!

Native\Compiled programs are faster than Interpreted languages, because they natively contain machine instructions, so, at every loop iteration there is no need to interpret\translate the loop's body script code because it is already in native machine instructions!

Ok, so the solution is to compile the AutoHotKey scripts and loop becomes fast, right? Wrong!

AutoHotKey compiled ".exe" files are not truly native\compiled programs! Here is the supposedly reason!

AHK "compiler" simply packages into the ".exe" files the AHK interpreter along with your source code (which is the script code and not the native machine instructions). So, when the ".exe" file is executed, AHK interpreter will still at every loop iteration interpret\translate the loop's body script code into native machine instructions in order to allow the processor to understand and execute it!

I don't know if the above is correct or wrong, but anyway, the question is, is there any "loop" faster alternative for AutoHotKey?
You have fortune 500 companies that operate large plants running AHK scripts. Some are very sensitive to timing and execution of code. Many companies do not realize that their contractors are using AHK to build things.

I personally have programs running all kinds of loops and never had an issue. Try replicating things in C++ and let me know if you see a difference.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: How to genuinely compile a script? Is there any "loop" faster alternative?

19 Nov 2017, 10:02

Prof Williams wrote:Try replicating things in C++ and let me know if you see a difference.
I wish I had the knowledge to do such thing!

Yes, loop is very very very slow when it must deal with millions iterations!

I wrote a string search function that searches for any specified string in an edit control and if the string is found, the function selects and shows the string!

There is 4 search options: Fixed (search while typing), Next, Previous and search by Occurrence (if 1, First occurrence, if 2, second occurrence, if L, Last Occurrence!)!

Saying that, the function works fast and great, but, let's say that an edit control contains 20 millions "A" characters, the function searches\selects\shows the first, the second, the third or the 1 million occurrence quickly and fast with no slowness! but I can't say the same for 5 millions or higher occurrences! For example, if I instruct the function to find the "L", which means last occurrence of "A" character, which in this example = 20 millions, the function would take forever to find it because the function's loop would have to iterate 20 million times! (I presume that the slowness occurs because the script code is not precompiled before execution, and because of that, the AHK interpreter would have to interpret\translate the function loop's body script code 20 million times!)

The function supports RegEx, which means that, if RegEx option is enabled, the function uses RegExMatch() function instead InStr() function! The slowness mentioned above occurs in both functions!
Guest

Re: How to genuinely compile a script? Is there any "loop" faster alternative?

19 Nov 2017, 11:24

Well why not change the logic of your script? Do so research as I'm sure you find examples on programming websites.

Does it help to rely on third party programs? For example would it be possible to store the 20 million into a text file, have a third party (command line) App search it and present the results back to AHK? There are some lightning fast command line search Apps available (for free) which you can just call and have a result back in only a few seconds.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: How to genuinely compile a script? Is there any "loop" faster alternative?

19 Nov 2017, 12:10

Guest wrote:Well why not change the logic of your script?
The logic of the script is great, there is nothing to change there!

I'm now very surprised, even loops with small bodies are really really slow!

In the example below, "if" statement and "loop" does exactly the same thing, they compare if "Text1" and "Text2" match or not each others! ("ïf" is truly fast, but "loop" really lags behind!)

if Text1 and Text2 contain 16 millions "A" characters, "if" takes only 63 milliseconds to compare them, but "loop" takes 12776 milliseconds (really slow)!

Code: Select all

#MaxMem 4095		;Allow 4095 MB per variable! A value larger than 4095 is considered to be 4095. A value less than 1 is considered to be 1.
SetBatchLines -1	;run script at maximum speed!


;"0x200" center text vertically (but "`n" or "`r`n" will not represent new lines)
gui, add, text, w400 r10 center 0x200 vWait, Defining Text1 and Text2 !!!
gui, show

loop, 16000000
Text1 .= "A"

Text2 := Text1

gui, hide
msgbox, Text1 and Text2 defined! Press ok to start comparison!
guicontrol, , Wait, Comparing Text1 and Text2 !!!
gui, show

Start := A_TickCount

if (Text1 = Text2)	;this if statement does exactly what the loop below does! (but it is fast!)
{
gui, hide
msgbox, % "Match (" A_TickCount - Start " milliseconds!)"
}
else
{
gui, hide
msgbox, % "Don't Match (" A_TickCount - Start " milliseconds!)"
}

gui, show

Start := A_TickCount

loop	;this loop does exactly what the "if" statment above does! (but it is slow!)
{
StartPos++

Char1 := SubStr(Text1, StartPos, 1)
Char2 := SubStr(Text2, StartPos, 1)

	if (Char1 != Char2)
	{
	gui, hide
	msgbox, % "Don't Match (" A_TickCount - Start " milliseconds!)"
	goto, guiclose
	}
	else if (Char1 = "" and Char2 = "")
	{
	gui, hide
	msgbox, % "Match (" A_TickCount - Start " milliseconds!)"
	goto, guiclose
	}
}


guiclose:
exitapp

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], flyingDman, frm1001xplrr, scriptor2016, sparqx, theelf and 165 guests