objects: temporary variables v. temporary array (speed)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

objects: temporary variables v. temporary array (speed)

18 Jul 2017, 16:19

In preparing to write a script to check for anagrams of a string, using multiple words, I was concerned re. using a temporary array v. multiple temporary variables (a pseudo-array). The code complexity is basically the same, and the speed is basically the same, although the true arrays actually seem to be slightly faster.

I posted this in case anyone has anything interesting to say about it, or spots some mistake in my test, or has any likely situations where variables/arrays might be significantly faster, or if these scripts can be optimised somewhat. Cheers.

Code: Select all

q:: ;speed for variables v. array seem to be roughly identical, arrays slightly faster
vCount := 100000
vText := "abcdefghijklmno"
vLen := StrLen(vText)

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

vTickCount1 := A_TickCount
vOutput1 := ""
VarSetCapacity(vOutput1, vCount*(StrLen(vText)+2)*2)
Loop, 5
	vTemp%A_Index% := ""
Loop, % vCount
{
	Loop, % vLen
	{
		vNum := Mod(A_Index-1,5)+1
		vTemp%vNum% .= SubStr(vText, A_Index, 1)
	}
	Loop, 5
		vOutput1 .= vTemp%A_Index%, vTemp%A_Index% := ""
	vOutput1 .= "`r`n"
}

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

vTickCount2 := A_TickCount
vOutput2 := ""
VarSetCapacity(vOutput2, vCount*(StrLen(vText)+2)*2)
oArray := {}
Loop, 5
	oArray[A_Index] := ""
Loop, % vCount
{
	Loop, % vLen
	{
		vNum := Mod(A_Index-1,5)+1
		oArray[vNum] .= SubStr(vText, A_Index, 1)
	}
	Loop, 5
		vOutput2 .= oArray[A_Index], oArray[A_Index] := ""
	vOutput2 .= "`r`n"
}

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

vTickCount3 := A_TickCount
oArray := ""
;MsgBox, % vOutput1
;MsgBox, % vOutput2
MsgBox, % (vOutput1 = vOutput2)
MsgBox, % (vTickCount2-vTickCount1) " " (vTickCount3-vTickCount2)
return
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 “Ask for Help (v1)”

Who is online

Users browsing this forum: Askeron52, haomingchen1998, SimmoF and 136 guests