Performance Question Regarding Multiple PixelGetColor Conditions

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hrdlywrkng
Posts: 5
Joined: 28 Dec 2017, 17:44

Performance Question Regarding Multiple PixelGetColor Conditions

27 May 2018, 04:33

Hello,

I have the following script which has a couple of PixelGetColor if/else conditions. It's definitely much slower the more conditions I put in there and I think i understand why, but is there a more optimized way of doing this to increase the performance of the script? Any help would be appreciated!

Code: Select all

#MaxThreadsPerHotkey 2
#NoEnv
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#KeyHistory 0
ListLines Off
Process, Priority, , A
SetBatchLines, -1
SetKeyDelay, -1, -1
SetMouseDelay, -1
SetDefaultMouseSpeed, 0
SetWinDelay, -1
SetControlDelay, -1
SendMode Input
CoordMode, Pixel, Window

Toggle = 0

{
$2::
    Toggle := !Toggle
    While Toggle {
        loop,
        {
            PixelGetColor, color1, 819, 850, RGB
            PixelGetColor, color2, 885, 866, RGB
            PixelGetColor, color3, 925, 859, RGB
            PixelGetColor, color4, 967, 859, RGB
            PixelGetColor, color5, 851, 854, RGB

            if (color1 = 0xFF0000)
            {
            	Send, {Numpad2}
                sleep 10
            	break
            }
            else if (color2 = 0xEED3C7)
            {
            	Send, {Numpad3}
                sleep 10
            	break
            }
            else if (color3 = 0xCA1008)
            {
            	Send, {Numpad4}
                sleep 10
            	break
            }
            else if (color4 = 0x52DC43)
            {
            	Send, {Numpad5}
                sleep 10
            	break
            }
            else if (color5 = 0x0A2A00)
            {
            	Send, {Numpad6}
                sleep 10
            	break
            }
            else {
                Send {Numpad1}
                sleep 10
                break
            }
        }
    }
    return
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Performance Question Regarding Multiple PixelGetColor Conditions

27 May 2018, 08:24

Besides using something else other than PixelGetColor to query pixel data?

How you've structured your script incurs a performance hit. You're first gathering all pixel colors, then start checking them iteratively to figure out what commands to carry out next. Those command appear to be mutually exclusive wrt. each other, as indicated by the breaks. So why do the work of querying 10 times if there's a possibility that you'll break on the 1st or 2nd or 3rd, etc found pixel? Instead query the pixels as you need them.

Here's the unmaintainable copy paste version:

Code: Select all

#MaxThreadsPerHotkey 2
#NoEnv
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#KeyHistory 0
ListLines Off
Process, Priority, , A
SetBatchLines, -1
SetKeyDelay, -1, -1
SetMouseDelay, -1
SetDefaultMouseSpeed, 0
SetWinDelay, -1
SetControlDelay, -1
SendMode Input
CoordMode, Pixel, Window

Toggle = 0

$2::
Toggle := !Toggle
While Toggle {
	PixelGetColor, foundColor, 819, 850, RGB
	if (foundColor = 0xFF0000)
	{
		Send, {Numpad2}
		sleep 10
		continue
	}
	
	PixelGetColor, foundColor, 885, 866, RGB
	if (foundColor = 0xEED3C7)
	{
		Send, {Numpad3}
		sleep 10
		continue
	}

	PixelGetColor, foundColor, 925, 859, RGB
	if (foundColor = 0xCA1008)
	{
		Send, {Numpad4}
		sleep 10
		continue
	}

	PixelGetColor, foundColor, 967, 859, RGB
	if (foundColor = 0x52DC43)
	{
		Send, {Numpad5}
		sleep 10
		continue
	}

	PixelGetColor, foundColor, 851, 854, RGB
	if (foundColor = 0x0A2A00)
	{
		Send, {Numpad6}
		sleep 10
		continue
	}

	Send {Numpad1}
	sleep 10
}
return
maybe shove pixelsearch in a function and save yourself some hassle:

Code: Select all

#MaxThreadsPerHotkey 2
#NoEnv
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#KeyHistory 0
ListLines Off
Process, Priority, , A
SetBatchLines, -1
SetKeyDelay, -1, -1
SetMouseDelay, -1
SetDefaultMouseSpeed, 0
SetWinDelay, -1
SetControlDelay, -1
SendMode Input
CoordMode, Pixel, Window

Toggle = 0

$2::
Toggle := !Toggle
While Toggle {
	if (getColorAt(819, 850) = 0xFF0000)
	{
		Send, {Numpad2}
		sleep 10
		continue
	}
	
	if (getColorAt(885, 866) = 0xEED3C7)
	{
		Send, {Numpad3}
		sleep 10
		continue
	}

	if (getColorAt(925, 859) = 0xCA1008)
	{
		Send, {Numpad4}
		sleep 10
		continue
	}

	if (getColorAt(967, 859) = 0x52DC43)
	{
		Send, {Numpad5}
		sleep 10
		continue
	}

	if (getColorAt(851, 854) = 0x0A2A00)
	{
		Send, {Numpad6}
		sleep 10
		continue
	}

	Send {Numpad1}
	sleep 10
}
return

getColorAt(x, y) {
	PixelGetColor, foundColor, % x, % y, RGB ; try Fast RGB
	return foundColor
}
for arbitrary number of colors:

Code: Select all

#MaxThreadsPerHotkey 2
#NoEnv
; #MaxHotkeysPerInterval 99000000 ; i
; #HotkeyInterval 99000000 ; wish
; #KeyHistory 0 ; this
; ListLines Off ; thread
; Process, Priority, , A ; would
SetBatchLines, -1
; SetKeyDelay, -1, -1 ; simply
; SetMouseDelay, -1 ; just
; SetDefaultMouseSpeed, 0 ; die
; SetWinDelay, -1 ; already, 
; SetControlDelay, -1 ; jesus
SendMode Input
CoordMode, Pixel, Window

class ColorKey
{
	__New(color, key, x, y)
	{
		this.color := color
		this.key := key
		this.x := x
		this.y := y
	}
}

ColorKeyMap := [new ColorKey(0xFF0000, "{Numpad2}", 819, 819)
			  , new ColorKey(0xEED3C7, "{Numpad3}", 885, 885)
			  , new ColorKey(0xCA1008, "{Numpad4}", 925, 925)
			  , new ColorKey(0x52DC43, "{Numpad5}", 967, 967)
			  , new ColorKey(0x0A2A00, "{Numpad6}", 851, 851)]

$2::
Toggle := !Toggle
while(Toggle)
{
	for each, Element in ColorKeyMap
	{
		PixelGetColor, foundColor, % Element.x, % Element.y, RGB ; maybe try 'Fast RGB' also

		if (Element.color = foundColor)
		{
			Send % Element.key
			Sleep 10
			continue 2 ; start a new iteration of the while loop
		}
	}

	; script has survived the foreach gauntlet
	; no colors were found, so send Num1 and start over
	Send {Numpad1}
	Sleep 10
}
return
hrdlywrkng
Posts: 5
Joined: 28 Dec 2017, 17:44

Re: Performance Question Regarding Multiple PixelGetColor Conditions

27 May 2018, 22:24

Thank you good sir! Makes a lot more sense now. Consider thread dead now!
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Performance Question Regarding Multiple PixelGetColor Conditions

28 May 2018, 01:06

That was a more of a jab at the thread you likely got your "header" from, rather than at yours specifically.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Nerafius, RandomBoy and 185 guests