JavaScript vs AutoHotKey (Simple Speed Test)

Discuss other programming languages besides AutoHotkey
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: JavaScript vs AutoHotKey (Simple Speed Test)

04 Feb 2019, 08:21

@nnnik: Can you see why the following code isn't working? The next step would be to replace MessageBoxW with towupper.

Code: Select all

;C++: C++ to machine code via TDM-GCC - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=23&t=49554
;MCode Tutorial (Compiled Code in AHK) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=7&t=32

q:: ;test call MessageBox
pFuncMessageBox := DllCall("kernel32\GetProcAddress", Ptr,DllCall("kernel32\GetModuleHandle", Str,"user32", Ptr), AStr,"MessageBoxW", Ptr)
DllCall(pFuncMessageBox, Ptr,0, Str,"prompt", Str,"title", UInt,0)
pFunc := MCode("2,x86:dABpAHQAbABlAAAAcAByAG8AbQBwAHQAIAAxAAAAcAByAG8AbQBwAHQAIAAyAAAAcAByAG8AbQBwAHQAIAAzAAAAAABTg+wYi1wkIMdEJAwAAAAAx0QkCAAAAADHRCQEDAAAAMcEJAAAAAD/04PsEMdEJAwAAAAAx0QkCAAAAADHRCQEHgAAAMcEJAAAAAD/04PsEMdEJAwAAAAAx0QkCAAAAADHRCQEMAAAAMcEJAAAAAD/04PsEIPEGFvD,x64:dABpAHQAbABlAAAAcAByAG8AbQBwAHQAIAAxAAAAcAByAG8AbQBwAHQAIAAyAAAAcAByAG8AbQBwAHQAIAAzAAAAAAAAAAAAAAAAAAAAAABTSIPsIEUxyUyNBQAAAABIjRUMAAAASInLMcn/00UxyTHJTI0FAAAAAEiNFR4AAAD/00yNBQAAAABIjRUwAAAARTHJMclIidhIg8QgW0j/4A==")
DllCall(pFunc, Ptr,pFuncMessageBox, "Cdecl")
SoundBeep
return

/*
void callmsgbox(int _stdcall(*MyFunction)(void*,short*,short*,unsigned int))
{
	//MessageBoxW(0, L"prompt", L"title", 0);
	(*MyFunction)(0, L"prompt 1", L"title", 0);
	(*MyFunction)(0, L"prompt 2", L"title", 0);
	(*MyFunction)(0, L"prompt 3", L"title", 0);
	return;
}
*/
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
nnnik
Posts: 4317
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: JavaScript vs AutoHotKey (Simple Speed Test)

04 Feb 2019, 08:27

You use strings that are static values. You cannot use static values in MCode - it might cause issues.
Recommends AHK Studio
User avatar
tank
Posts: 2772
Joined: 28 Sep 2013, 22:15
Facebook: charlie.simmons.7334
Google: ttnnkkrr
GitHub: ttnnkkrr
Location: Irving TX
Contact:

Re: JavaScript vs AutoHotKey (Simple Speed Test)

04 Feb 2019, 09:36

Comparing ECMA speed which has been optimized for 30+ years and AHK is both idiotic and trollish. Comparing speed of a compiled language to an interpreted one is similarly idiotic and trollish. What honestly is your point? AHK is an automation language. Unless one of you is an expert at RPA like I am, i doubt your competent to compare automation language performance. As automation tools go, i dont know of a faster one than AHK( i will admit to not knowing EVERYTHING). The bottleneck with automation languages is however the speed of the desktop application or browser page being automated. in this case automation language speed is nearly irrelevant.

AHK is only suitable as a generalized scripting language because of it's ease to learn and use. Otherwise i would rather Python or ECMA.

I am tempted to lock this debate but i am honestly curious how stupid it can get.
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
https://www.facebook.com/ahkscript.org
If you have forum suggestions please submit a pull request
Check Out WebWriter
Thanks Tank :thumbup:
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: JavaScript vs AutoHotKey (Simple Speed Test)

04 Feb 2019, 10:10

SOTE wrote:
04 Feb 2019, 06:59
My friend, it's clear here that you are a troll trolling all the time on this matter and it is even more clear that you have big problems in understand things! (thus the reason why you are trolling!)

Listen Mister troll,

- People use AHK and others scripting languages in order to write simple, short and readable codes! (Fact! You argue this because you are a troll!)

- AHK loop is slow to very slow, so people are automatically forced to write complex, longer and unreadable codes in order to avoid it! (Fact! You argue this because you are a troll!)

If people are forced to write complex, longer and unreadable codes in order to make AHK loop a little bit more efficient, well, there is no point to use AHK anymore in what concerns loop (in which is needed most of the time)!

Worst, there are scripting languages out there that do real compilation, like Javascript for example (in which for(;;) loop is 19x faster than AHK loop), allowing people to write simple, short and readable codes without worrying about loop iteration!

And my friend Mister troll, remember that, optimizations can be applied to any languages! Yes, I can apply optimizations to Javascript for example by writing complex, longer and unreadable codes, in which makes AHK lags behind even more!
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: JavaScript vs AutoHotKey (Simple Speed Test)

04 Feb 2019, 11:36

tank wrote:
04 Feb 2019, 09:36
Comparing ECMA speed which has been optimized for 30+ years and AHK is both idiotic and trollish. Comparing speed of a compiled language to an interpreted one is similarly idiotic and trollish. What honestly is your point? AHK is an automation language. Unless one of you is an expert at RPA like I am, i doubt your competent to compare automation language performance. As automation tools go, i dont know of a faster one than AHK( i will admit to not knowing EVERYTHING). The bottleneck with automation languages is however the speed of the desktop application or browser page being automated. in this case automation language speed is nearly irrelevant.

AHK is only suitable as a generalized scripting language because of it's ease to learn and use. Otherwise i would rather Python or ECMA.

I am tempted to lock this debate but i am honestly curious how stupid it can get.
"Unless one of you is an expert at RPA like I am" (Haha! Lol!)

Hehe, go on and lock this thread! (I'm so afraid of your stupidity ...!) Image
SOTE
Posts: 837
Joined: 15 Jun 2015, 06:21

Re: JavaScript vs AutoHotKey (Simple Speed Test)

05 Feb 2019, 02:05

User wrote:
31 Jan 2019, 18:58
Wow, now I am really surprised!

I wrote "Is()" function (click here) for AutoHotKey just to test speed! (which is really slow, it takes 14000 milliseconds (14 seconds) to compare 2 strings of ~= 20 megabytes each!)

I decided to write the same function for JavaScript (almost a clone), and I was expecting it to be 10x to 20x slower than "Is()" function from AHK!

Guess what, "Is()" function from JavaScript takes only 741 milliseconds (less than 1 second) to compare 2 strings of ~= 20 megabytes each!
Speed of AutoHotkey 3X faster than your JavaScript example. 235 milliseconds to your 741 milliseconds. Improved script by nnnik.
Image

It's quite telling that you ignored the request and improved script by nnnik.
@User then again I posted an example which is extremly fast.
Could you implement the same algorythm in javascript to see it's speed?
Which begs the question, how fast is fast enough? Wouldn't it depend on what the script is being used for and if the user is satisfied that the script accomplishes what they needed? The unsatisfied user is free to improve their script (however they see fit), add requests to update the AutoHotkey source code, seek to mix in code from other programming languages, or use other programming languages, but let's be clear that's an individual decision and based on their problem.

Code: Select all

x := StrReplace(Format("{:20100100}", ""), " ", "a")
y := x . "A"
x .= "B"
SetBatchLines, -1
qSortErrorLookup(x, y)
qSortErrorLookup(x, y)
qSortErrorLookup(x, y)
;a few dry runs
start := A_TickCount
difference := qSortErrorLookup(x, y)
total := A_TickCount - start
Msgbox % Clipboard := "Found difference at character position: " . difference . "`ntook " . total . " ms"

;Found difference at character position: 20100101
;took 156 ms
;Found difference at character position: 20100101
;took 141 ms
;Found difference at character position: 20100101
;took 172 ms

qSortErrorLookup(byref string1, byref string2, partCount := 2) {
	Local
	if (string1 = string2) {
		return false
	}
	
	cLeftBorder := 1
	cLen := max(strLen(string1), strLen(string2))
	loop {
		stepSize := (cLen)/partCount
		leftBorder := cLeftBorder
		loop %partCount% {
			leftB := round(leftBorder)
			len := Round((leftBorder+=stepSize)-leftB)
			if (subStr(string1, leftB, len) != subStr(string2, leftB, len)) {
				if (len = 1)
					return leftB
				cLeftBorder := leftB
				cLen := len
				break
			}
		}
	}
	return false
}
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: JavaScript vs AutoHotKey (Simple Speed Test)

07 Feb 2019, 12:54

- I tried writing machine code for this but it wasn't particularly fast. Any suggestions/improvements are welcome.

Code: Select all

;[MCode function]
;GitHub - joedf/MCode4GCC: MCode4GCC is an MCode generator using the GCC Compiler.
;https://github.com/joedf/MCode4GCC

q:: ;string compare case-insensitive Unicode get position
;VarSetCapacity(x, 20100100*2+2)
;Loop, 20100100
;	x .= "a"
x := StrReplace(Format("{:20100100}", ""), " ", "a")
y := x
x .= "A"
y .= "a"

;NumPut(Ord("z"), &x, 10000000, "UShort")

Clipboard .= "`r`n" "BENCHMARK TEST:"
vSize := StrLen(x)*2
MsgBox, % "vars ready"

;case-sensitive comparison
vTickCount := A_TickCount
vRet := DllCall("ntdll\RtlCompareMemory", Ptr,&x, Ptr,&y, UPtr,vSize, UPtr)
Clipboard .= "`r`n" (A_TickCount - vTickCount)
MsgBox, % vRet " " (vRet = vSize)

;case-insensitive comparison
vTickCount := A_TickCount
StringLower, x, % x
StringLower, y, % y
vRet := DllCall("ntdll\RtlCompareMemory", Ptr,&x, Ptr,&y, UPtr,vSize, UPtr)
Clipboard .= "`t" (A_TickCount - vTickCount)
MsgBox, % vRet " " (vRet = vSize)

;case-insensitive comparison
pFuncToWUpper := DllCall("kernel32\GetProcAddress", Ptr,DllCall("kernel32\GetModuleHandle", Str,"msvcrt", Ptr), AStr,"towupper", Ptr)
pFunc := MCode("2,x86:VVdWU4PsHItEJDiLbCQ0i1wkPIXAdDMx/+sOkI10JgCDxwE5fCQ4dCGLRCQwD7cEeIkEJP/TicYPt0R9AIkEJP/TOcZ02ol8JDiLRCQ4g8QcW15fXcM=,x64:QVZBVUFUVVdWU0iD7CBFhcBJicxJidVFicZMic50MkGNaP8x20iDxQHrDg8fRAAASIPDAUg53XQYQQ+3DFz/1kEPt0xdAInH/9Y5x3TiQYneRInwSIPEIFteX11BXEFdQV7D")
vTickCount := A_TickCount
vRet := DllCall(pFunc, Ptr,&x, Ptr,&y, UInt,vSize/2, Ptr,pFuncToWUpper, "Cdecl")
Clipboard .= "`t" (A_TickCount - vTickCount)
MsgBox, % vRet " " (vRet = vSize/2)
return

;e.g. results
;15	203	780
;31	203	780
;15	202	764
return

/*
unsigned int customstrcmpi(unsigned short* str1, unsigned short* str2, unsigned int count, int _cdecl(*MyFunction)(int))
{
	for (unsigned int i = 0; i < count; ++i)
	{
		//if (str1[i] != str2[i])
		if ((*MyFunction)(str1[i]) != (*MyFunction)(str2[i]))
		{
			count = i;
			break;
		}
	}
	return count;
}
*/
- I fixed the 'call MessageBox' example (at the top of this page), here:
C++: TDM-GCC: error with bool - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=23&t=59979&p=262531#p262531
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
nnnik
Posts: 4317
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: JavaScript vs AutoHotKey (Simple Speed Test)

07 Feb 2019, 13:01

How does it compare to my code? Also you need to copy the string when using StrLower - this might further decrease the performance of your method.
Recommends AHK Studio
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: JavaScript vs AutoHotKey (Simple Speed Test)

07 Feb 2019, 14:41

- The machine code doesn't use StrLower, in the code example above I use 3 methods.
- These are the results.
case sensitive:
15 RtlCompareMemory

case insensitive:
202 StringLower + RtlCompareMemory
203 SubStr (divide and conquer)
655 Format + RtlCompareMemory
780 machine code
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
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: JavaScript vs AutoHotKey (Simple Speed Test)

15 Feb 2019, 21:51

@gregster: ß/ss is mentioned in these links. Also, Turkish i, another classic, is mentioned in the second link.
towupper - cppreference.com
https://en.cppreference.com/w/c/string/wide/towupper
java - How do I make my string comparison case insensitive? - Stack Overflow
https://stackoverflow.com/questions/2220400/how-do-i-make-my-string-comparison-case-insensitive
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
nnnik
Posts: 4317
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: JavaScript vs AutoHotKey (Simple Speed Test)

16 Feb 2019, 04:11

First time I hear about ß being turned into SS. Imo it's either a flaw a bug or yeah something like that
Recommends AHK Studio
gregster
Posts: 3379
Joined: 30 Sep 2013, 06:48

Re: JavaScript vs AutoHotKey (Simple Speed Test)

16 Feb 2019, 07:07

nnnik wrote:
16 Feb 2019, 04:11
First time I hear about ß being turned into SS. Imo it's either a flaw a bug or yeah something like that
It's pretty natural, that ss is used for ß...
https://www.autohotkey.com/boards/viewtopic.php?f=23&t=61602&start=20#p261456 wrote:Then again, until 1996's spelling reform it was Kuß instead of Kuss (in Germany) - while in Liechtenstein and Switzerland, german-speaking people never use ß - always ss.
A living language changes over time. Only dead languages never change... in 1996 and after, people hated the spelling reform and made fun of it - today, younger people aren't even aware that there was one...

Of course, the mentioned uppercase version of ß (not available on my german keyboard) is still pretty new uncommon - I think it is mainly motivated by typographical reasonings (especially if you use all capitalized letters) - then, depending on the font, a lowercase ß often looks out of place (that's why people often used SS or SZ (!) instead - the latter one is not used anymore)
guest3456
Posts: 2774
Joined: 09 Oct 2013, 10:31

Re: JavaScript vs AutoHotKey (Simple Speed Test)

17 Feb 2019, 18:20

tank wrote:
04 Feb 2019, 09:36
I am tempted to lock this debate but i am honestly curious how stupid it can get.
this "User" user has been just as stupid in other threads as well.

User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: JavaScript vs AutoHotKey (Simple Speed Test)

23 Oct 2019, 09:47

I have a method which may be 4 times faster than the previous best method at around 47 msec.
Previous results for comparison:
JavaScript vs AutoHotKey (Simple Speed Test) - Page 3 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=23&t=61602&p=262559#p262559

The idea is to use _wcsnicmp/_strnicmp to compare blocks of characters, and then to home in on the first differing character within that block. The approach is described in the comments.

There's a lot going on in the function, so do notify of any issues. Cheers.

Code: Select all

q:: ;compare strings case insensitive (and get 1-based char index where differ)
vText1 := StrReplace(Format("{:20100100}", ""), " ", "a")
vText2 := vText1
vText1 .= "A"
vText2 .= "b"
;vText2 .= "a"

;vText1 := "abc", vText2 := "abd"
;vText1 := "abc", vText2 := "abcd"
;vText1 := "abc", vText2 := "abC"
;JEE_Swap(vText1, vText2)

vTickCount1 := A_TickCount
vRet := StrCmpI(vText1, vText2,, vOffsetDiff)
vTickCount2 := A_TickCount
MsgBox, % Clipboard := (vTickCount2 - vTickCount1) "`r`n" vRet " " vOffsetDiff
return

;e.g. results:
;47
;47
;31

;==================================================

;principles:
;use _wcsnicmp/_strnicmp to compare blocks of characters, find the first difference
;use memmove to copy blocks
;use CharLowerBuff to make them lower case
;use RtlCompareMemory to compare characters, find the first difference (its char index)

;note: vOffsetParam is for use with comparator functions for AHK's Sort command
;note: vText1/vText2 are ByRef for better performance (to avoid copying massive strings)
StrCmpI(ByRef vText1, ByRef vText2, vOffsetParam:=0, ByRef vOffsetOut:=0)
{
	local
	static vChrSize := A_IsUnicode ? 2 : 1
	static vFunc := A_IsUnicode ? "msvcrt\_wcsnicmp" : "msvcrt\_strnicmp"
	static vBlockLen := 1000000
	vLen1 := StrLen(vText1)
	vLen2 := StrLen(vText2)
	if !(vLen := Min(vLen1, vLen2))
		return -vOffsetParam ;return 0/-vOffsetParam if both empty strings
	vRem := Mod(vLen, vBlockLen)
	vOffset := vOffsetOut := vRet := 0
	Loop % Floor(vLen/vBlockLen)
	{
		if vRet := DllCall(vFunc, "Ptr",&vText1+vOffset*vChrSize, "Ptr",&vText2+vOffset*vChrSize, "Ptr",vBlockLen, "Cdecl")
			break
		vOffset += vBlockLen
	}
	if !vRet && (vBlockLen := vRem)
		vRet := DllCall(vFunc, "Ptr",&vText1+vOffset*vChrSize, "Ptr",&vText2+vOffset*vChrSize, "Ptr",vRem, "Cdecl")
	if !vRet
	{
		if (vLen1 = vLen2)
			return -vOffsetParam ;return 0/-vOffsetParam if equal
		vOffsetOut := vLen + 1
		return (vLen1 > vLen2) ? 1 : -1
	}
	VarSetCapacity(vTemp1, vBlockLen*vChrSize)
	VarSetCapacity(vTemp2, vBlockLen*vChrSize)
	DllCall("msvcrt\memmove", "Ptr",&vTemp1, "Ptr",&vText1+vOffset*vChrSize, "UPtr",vBlockLen*vChrSize, "Cdecl Ptr")
	DllCall("msvcrt\memmove", "Ptr",&vTemp2, "Ptr",&vText2+vOffset*vChrSize, "UPtr",vBlockLen*vChrSize, "Cdecl Ptr")
	DllCall("user32\CharLowerBuff", "Ptr",&vTemp1, "UInt",vBlockLen, "UInt")
	DllCall("user32\CharLowerBuff", "Ptr",&vTemp2, "UInt",vBlockLen, "UInt")
	vOffset2 := DllCall("ntdll\RtlCompareMemory", "Ptr",&vTemp1, "Ptr",&vTemp2, "UPtr",vBlockLen*vChrSize, "UPtr")
	;note: use Floor, since, for a Unicode string, the first byte that differs could be at an odd offset:
	vOffsetOut := vOffset + Floor(vOffset2/vChrSize) + 1
	return (vRet > 0) ? 1 : -1
}

;==================================================
[EDIT:] Simplified the function. And corrected RtlCompareMemory to point to the right locations.
[EDIT:] Note: if I run the script around half-a-dozen times, it starts to slow down!?
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Other Programming Languages”

Who is online

Users browsing this forum: No registered users and 3 guests