Skip to content

samfisherirl/Compare-Code-Speed-GUI-AHKv2-Optimize-Scripts-For-Speed

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Optimize AHKv2 for speed

Optimize AHKv2 Code for Speed

image

Credits:

I'm not going to try to rewrite the original post text, a lot of it is great and should be read from the source https://www.autohotkey.com/boards/viewtopic.php?f=7&t=6413 Many of the conventions such as #NoEnv and #SetBatchLines are defunct, but the rest is very valuable. I have restructured the tests for ahkv2.

tests include:

  • a few tips for IF checking of Boolean values
  • Avoid spreading math over multiple lines and using variable for intermediate results if they are only used once. As much as possible condense math into one line and only use variables for storing math results if you need the results being used multiple times later.
  • Terinary
  • Variable expressions

Commas and Combined lines aren't faster in v2; with caveats.

v2 Guide / Tutorial

Over the course of 3 months, I've learned some optimization tricks. After a deeper dive and some reflection, I've found I had some misconceptions on their transient ability in moving to AHKv2. I noticed a lot of this syntax in experienced community members' scripts, including mine, and I want to share what I've found.

https://github.com/samfisherirl/Compare-Code-Speed-GUI-AHKv2

The tests

Defining variables by lines and commas

tests are shortened for brevity

;test 1
t1a := 1
t1b := 1
t1c := 1
t1d := 1

;test 2
 t2f := t2g := t2h := t2i := t2j := 1

;test3
t3a := 1, t3b := 1, t3c := 1, t3d := 1

AHKv1 results =

	;test1 0.240315

	;test2 0.132753

	;test3 0.168953

ahkv2 results =

	 ;test1 0.00124844 (50% + faster)

	;test2 0.00259254

	;test3 0.00274485

We can see combining variables on a single line in these examples are no longer faster but hamper the code. We'll find out this is different with function calls.

Let's do it again with functions

these functions are across all tests ; condensed

	e() {   y := 999*222
	   return y }

	f() {  y := 999*222
	   return y }
	g() {   y := 999*222
	   return y }

test1

	a := e()
	b := f()
	c := g()

test2

a := e(),b := f(),c := g()

test3

    a := e()
,b := f()
,c := g()

results

	;test1 0.01627 (50% slower)
	;test2 0.01098
	;test3 0.011008

Even shortened conditionals aren't faster with combined lines

;test1

x := true

if x
   z:=1, a:=2, b:=1, c:=2

;test2

	x := true

	if x
	{
	   z:=1
	   a:=2
	   b:=1
	   c:=2
	}
  • test1 0.0026

  • test2 0.00180 ;30% faster