Threads slow each other down (not hardware related) Topic is solved

Post AHK_H specific scripts & libraries and discuss the usage and development of HotKeyIt's fork/branch
kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

Threads slow each other down (not hardware related)

01 May 2019, 00:09

Hello, for some reason that I can't figure out, my threads are slowing down too much.

Code: Select all

CritObj := CriticalObject()
CritObj.found:=1
threads=
(%`
#persistent
#notrayicon
#NoEnv
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#KeyHistory 0
ListLines Off
Coordmode, pixel, screen
Coordmode, mouse, screen
Coordmode, tooltip, screen
asd:=0
loop
{
	if (CritObj.found=0)
	{
		starttime:=a_tickcount
		ImageSearch, asdx, asdy, 0, 0, 100, 100, %Image%
		tooltip, % a_tickcount-starttime, % number*50
		break
	}
	sleep, 10
}
return
)

loop, 20
	asd := AhkThread("CritObj:=CriticalObject(" &CritObj ")`nImage=test" a_index ".png`nnumber:=" a_index "`n" threads)
return

f2::
CritObj.found:=0
return
That's my code. It will launch all threads that have a loop waiting for me to change CritObj.found to 0.
When I change CritObj.found to 0 those threads perform an image search and then break and sleep because they are #persistent. They will show the result in tooltips.

For some reason the time it takes for each thread to perform the imagesearch is huge compared to 1 imagesearch performed with an ahk script.

Doing the same Imagesearch once on 1 normal script takes: 11ms
Doing the same Imagesearch with 5 threads at the same time takes: average of 62ms each
Doing the same Imagesearch with 10 threads at the same time takes: average of 86ms each
Doing the same Imagesearch with 20 threads at the same time takes: average of 175ms each

This is not hardware related as I'm using an i5 8400, gtx 1060 6gb, 16gb ram and an ssd. The CPU Consumption doesn't go past 5%

Can Imagesearch be causing the issue? Like multiple imagesearches are slowing each other? or is this something related to threads slowing each other?

Thanks in advance
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Threads slow each other down (not hardware related)

01 May 2019, 03:25

i think its because ure mutex locking the critical object every time its properties are read. the more threads, the more locking and synchronizing needed, the slower it becomes. a better approach is to start ur threads in a paused state and resume them from the main script when needed
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Threads slow each other down (not hardware related)

01 May 2019, 07:47

Can you try running 5 or 20 exe processes doing the same, does it also slow down?
kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

Re: Threads slow each other down (not hardware related)  Topic is solved

01 May 2019, 12:00

HotKeyIt wrote:
01 May 2019, 07:47
Can you try running 5 or 20 exe processes doing the same, does it also slow down?
Ran some tests I wish I had done before posting :D
Here are the results:

Running 10 scripts with this code:
Script opener.ahk

Code: Select all

f3::
mousegetpos, x, y
loop, 10
	run, % "search.ahk " x " " y " test" a_index ".png"
return
search.ahk:

Code: Select all

#SingleInstance off
#notrayicon
#NoEnv
#KeyHistory 0
ListLines Off
SetBatchLines, -1
SetDefaultMouseSpeed, 0
Coordmode, pixel, screen
Coordmode, mouse, screen
Coordmode, tooltip, screen
x1=%1%
x1:=x1-50
x2:=x1+100
y1:=%2%
y1:=y1-50
y2:=y1+100
return

~f1::
qpx(true)
loop, 50
	ImageSearch, asdx, asdy, %x1%, %y1%, %x2%, %y2%, %3%
if (errorlevel=2)
	msgbox a
msgbox % qpx()/50
return


QPX( N=0 )
{ ; Wrapper for QueryPerformanceCounter()by SKAN | CD: 06/Dec/2009
	Static F,A,Q,P,X ; www.autohotkey.com/forum/viewtopic.php?t=52083 | LM: 10/Dec/2009
	If	( N && !P )
		Return	DllCall("QueryPerformanceFrequency",Int64P,F) + (X:=A:=0) + DllCall("QueryPerformanceCounter",Int64P,P)
	DllCall("QueryPerformanceCounter",Int64P,Q), A:=A+Q-P, P:=Q, X:=X+1
	Return	( N && X=N ) ? (X:=X-1)<<64 : ( N=0 && (R:=A/X/F) ) ? ( R + (A:=P:=X:=0) ) : 1
}

~f5::exitapp
Doing the same Imagesearch with 10 SCRIPTS opened at the same time takes: average of 170ms each
AHK_H took an average of 86ms each.

So it seems that the problem is Imagesearch.

I adapted my code to use GDIP Imagesearch and the results are AMAZING.
Doing the same Imagesearch with 10 SCRIPTS opened at the same time using GDIP takes: average of 0.1ms each. Yes 0.1ms not 0.1seconds, it's amazing.

Doing the same Imagesearch with 10 threads at the same time using GDIP takes: average of 0.15ms each. Yes again, 0.15ms not 0.15seconds.

So it seems Imagesearch is the problem.
User avatar
manehscripts
Posts: 126
Joined: 03 May 2019, 16:10

Re: Threads slow each other down (not hardware related)

30 Jul 2019, 12:06

kyuuuri wrote:
01 May 2019, 12:00
HotKeyIt wrote:
01 May 2019, 07:47
Can you try running 5 or 20 exe processes doing the same, does it also slow down?
Ran some tests I wish I had done before posting :D
Here are the results:

Running 10 scripts with this code:
Script opener.ahk

Code: Select all

f3::
mousegetpos, x, y
loop, 10
	run, % "search.ahk " x " " y " test" a_index ".png"
return
search.ahk:

Code: Select all

#SingleInstance off
#notrayicon
#NoEnv
#KeyHistory 0
ListLines Off
SetBatchLines, -1
SetDefaultMouseSpeed, 0
Coordmode, pixel, screen
Coordmode, mouse, screen
Coordmode, tooltip, screen
x1=%1%
x1:=x1-50
x2:=x1+100
y1:=%2%
y1:=y1-50
y2:=y1+100
return

~f1::
qpx(true)
loop, 50
	ImageSearch, asdx, asdy, %x1%, %y1%, %x2%, %y2%, %3%
if (errorlevel=2)
	msgbox a
msgbox % qpx()/50
return


QPX( N=0 )
{ ; Wrapper for QueryPerformanceCounter()by SKAN | CD: 06/Dec/2009
	Static F,A,Q,P,X ; www.autohotkey.com/forum/viewtopic.php?t=52083 | LM: 10/Dec/2009
	If	( N && !P )
		Return	DllCall("QueryPerformanceFrequency",Int64P,F) + (X:=A:=0) + DllCall("QueryPerformanceCounter",Int64P,P)
	DllCall("QueryPerformanceCounter",Int64P,Q), A:=A+Q-P, P:=Q, X:=X+1
	Return	( N && X=N ) ? (X:=X-1)<<64 : ( N=0 && (R:=A/X/F) ) ? ( R + (A:=P:=X:=0) ) : 1
}

~f5::exitapp
Doing the same Imagesearch with 10 SCRIPTS opened at the same time takes: average of 170ms each
AHK_H took an average of 86ms each.

So it seems that the problem is Imagesearch.

I adapted my code to use GDIP Imagesearch and the results are AMAZING.
Doing the same Imagesearch with 10 SCRIPTS opened at the same time using GDIP takes: average of 0.1ms each. Yes 0.1ms not 0.1seconds, it's amazing.

Doing the same Imagesearch with 10 threads at the same time using GDIP takes: average of 0.15ms each. Yes again, 0.15ms not 0.15seconds.

So it seems Imagesearch is the problem.
Hello,

I may be wrong, but it seems to me that QPX only converts "ms" to "0.0...." Look at this example below, it makes a comparison and the work is the same.
Please let me know if I did something wrong?

Code: Select all

#NoEnv
#SingleInstance off
#KeyHistory 0
ListLines Off
SetBatchLines, -1
SetDefaultMouseSpeed, 0
Gui, +AlwaysOnTop
Gui, Add, Button, h50 w250 gStart, SEARCH
Gui, Show, `t
return

Start:
	Coordmode, pixel, screen
	Coordmode, mouse, screen
	Coordmode, tooltip, screen
	Loop, 100
	{
		t1 := A_TickCount
		QPX(true)
		ImageSearch, asdx, asdy, 0, 0, 2000, 2000, %A_ScriptDir%\test1.png
		if (errorlevel==0) {
			ms := A_TickCount-t1
			qpx := % QPX()
			ToolTip, %qpx% - %ms%ms
		} else {
			ToolTip, Not found!
		}
	}
	ToolTip
return

QPX( N=0 )
{ ; Wrapper for QueryPerformanceCounter()by SKAN | CD: 06/Dec/2009
	Static F,A,Q,P,X ; www.autohotkey.com/forum/viewtopic.php?t=52083 | LM: 10/Dec/2009
	If	( N && !P )
		Return	DllCall("QueryPerformanceFrequency",Int64P,F) + (X:=A:=0) + DllCall("QueryPerformanceCounter",Int64P,P)
	DllCall("QueryPerformanceCounter",Int64P,Q), A:=A+Q-P, P:=Q, X:=X+1
	Return	( N && X=N ) ? (X:=X-1)<<64 : ( N=0 && (R:=A/X/F) ) ? ( R + (A:=P:=X:=0) ) : 1
}

Esc::ExitApp
GuiClose:
ExitApp
-----------------------------------------------------------
Stop to think, shut up to resist, and act to win!

Return to “AutoHotkey_H”

Who is online

Users browsing this forum: No registered users and 8 guests