How can I make this faster?

Get help with using AutoHotkey and its commands and hotkeys
Spiralio
Posts: 4
Joined: 04 Dec 2017, 03:13

How can I make this faster?

Post by Spiralio » 10 Jan 2018, 19:21

Ok, so this is my code:

Code: Select all

f10::
SetMouseDelay -1
SetDefaultMouseSpeed, 0
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
; 1
ImageSearch, OpX, OpY, 0, 0, 1920, 1080, Op1.PNG
If (Errorlevel = 0){
	ImageSearch, RX, RY, 0, 0, 1920, 1080, R1.PNG
	MouseClickDrag, Left, OpX, OpY, RX, RY
}
; 2
ImageSearch, OpX, OpY, 0, 0, 1920, 1080, Op2.PNG
If (Errorlevel = 0){
	ImageSearch, RX, RY, 0, 0, 1920, 1080, R2.PNG
	MouseClickDrag, Left, OpX, OpY, RX, RY
}
; 3
ImageSearch, OpX, OpY, 0, 0, 1920, 1080, Op3.PNG
If (Errorlevel = 0){
	ImageSearch, RX, RY, 0, 0, 1920, 1080, R3.PNG
	MouseClickDrag, Left, OpX, OpY, RX, RY
}
; 4
ImageSearch, OpX, OpY, 0, 0, 1920, 1080, Op4.PNG
If (Errorlevel = 0){
	ImageSearch, RX, RY, 0, 0, 1920, 1080, R4.PNG
	MouseClickDrag, Left, OpX, OpY, RX, RY
}
; 5
ImageSearch, OpX, OpY, 0, 0, 1920, 1080, Op5.PNG
If (Errorlevel = 0){
	ImageSearch, RX, RY, 0, 0, 1920, 1080, R5.PNG
	MouseClickDrag, Left, OpX, OpY, RX, RY
}
; 6
ImageSearch, OpX, OpY, 0, 0, 1920, 1080, Op6.PNG
If (Errorlevel = 0){
	ImageSearch, RX, RY, 0, 0, 1920, 1080, R6.PNG
	MouseClickDrag, Left, OpX, OpY, RX, RY
}
; 7
ImageSearch, OpX, OpY, 0, 0, 1920, 1080, Op7.PNG
If (Errorlevel = 0){
	ImageSearch, RX, RY, 0, 0, 1920, 1080, R7.PNG
	MouseClickDrag, Left, OpX, OpY, RX, RY
}
; 8
ImageSearch, OpX, OpY, 0, 0, 1920, 1080, Op8.PNG
If (Errorlevel = 0){
	ImageSearch, RX, RY, 0, 0, 1920, 1080, R8.PNG
	MouseClickDrag, Left, OpX, OpY, RX, RY
}
; 9
ImageSearch, OpX, OpY, 0, 0, 1920, 1080, Op9.PNG
If (Errorlevel = 0){
	ImageSearch, RX, RY, 0, 0, 1920, 1080, R9.PNG
	MouseClickDrag, Left, OpX, OpY, RX, RY
}
; 10
ImageSearch, OpX, OpY, 0, 0, 1920, 1080, Op10.PNG
If (Errorlevel = 0){
	ImageSearch, RX, RY, 0, 0, 1920, 1080, R10.PNG
	MouseClickDrag, Left, OpX, OpY, RX, RY
}
; 11
ImageSearch, OpX, OpY, 0, 0, 1920, 1080, Op11.PNG
If (Errorlevel = 0){
	ImageSearch, RX, RY, 0, 0, 1920, 1080, R11.PNG
	MouseClickDrag, Left, OpX, OpY, RX, RY
}
; 12
ImageSearch, OpX, OpY, 0, 0, 1920, 1080, Op12.PNG
If (Errorlevel = 0){
	ImageSearch, RX, RY, 0, 0, 1920, 1080, R12.PNG
	MouseClickDrag, Left, OpX, OpY, RX, RY
}
; 13
ImageSearch, OpX, OpY, 0, 0, 1920, 1080, Op13.PNG
If (Errorlevel = 0){
	ImageSearch, RX, RY, 0, 0, 1920, 1080, R13.PNG
	MouseClickDrag, Left, OpX, OpY, RX, RY
}
; 14
ImageSearch, OpX, OpY, 0, 0, 1920, 1080, Op14.PNG
If (Errorlevel = 0){
	ImageSearch, RX, RY, 0, 0, 1920, 1080, R14.PNG
	MouseClickDrag, Left, OpX, OpY, RX, RY
}
Return
What it does, is check for image 1, then check for image 2, and drags image 1 to image 2.
There are 28 images in total, which means there are 14 modules. How can I make this more compact, and hopefully smaller?

User avatar
KuroiLight
Posts: 327
Joined: 12 Apr 2015, 20:24
GitHub: KuroiLight

Re: How can I make this faster?

Post by KuroiLight » 10 Jan 2018, 20:32

put the image filenames in an array and use a for loop. won't be any faster performance-wise but will be shorter code-wise.
Windows 10, Ryzen 1600, 16GB G.Skill DDR4, 8GB RX 480 | [MyScripts][MySublimeSettings] [Unlicense][MIT License]
01/24/18
[/color]

User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: How can I make this faster?

Post by Exaskryz » 10 Jan 2018, 20:35

Your code looks like it can be reduced using A_Index:

Code: Select all

Loop, 14
{
ImageSearch, OpX, OpY, 0, 0, 1920, 1080, Op%A_Index%.PNG
If (Errorlevel = 0){
	ImageSearch, RX, RY, 0, 0, 1920, 1080, R%A_Index%.PNG
	MouseClickDrag, Left, OpX, OpY, RX, RY
}
However, I will note that you don't have another If statement for images R1-14 and an attempt to drag will be done if the R image isn't found. And since you repeat variables, consider this scenario:

OP1 is found. R1 is found. Drags from Op1 to R1.
OP2 is found. R2 is NOT found. Because RX and RY never got changed, they still match R1's coordinate.s Now it drags from OP2 to R1.

---

Not sure about how to improve speed overall. SetBatchLines, -1 is probably the last suggestion I have for what I see people commonly put in their code when trying to make it as fast as possible.

Spiralio
Posts: 4
Joined: 04 Dec 2017, 03:13

Re: How can I make this faster?

Post by Spiralio » 10 Jan 2018, 21:17

Exaskryz wrote:Your code looks like it can be reduced using A_Index:

Code: Select all

Loop, 14
{
ImageSearch, OpX, OpY, 0, 0, 1920, 1080, Op%A_Index%.PNG
If (Errorlevel = 0){
	ImageSearch, RX, RY, 0, 0, 1920, 1080, R%A_Index%.PNG
	MouseClickDrag, Left, OpX, OpY, RX, RY
}
However, I will note that you don't have another If statement for images R1-14 and an attempt to drag will be done if the R image isn't found. And since you repeat variables, consider this scenario:

OP1 is found. R1 is found. Drags from Op1 to R1.
OP2 is found. R2 is NOT found. Because RX and RY never got changed, they still match R1's coordinate.s Now it drags from OP2 to R1.

---

Not sure about how to improve speed overall. SetBatchLines, -1 is probably the last suggestion I have for what I see people commonly put in their code when trying to make it as fast as possible.
Okay, thanks! The thing is, that there is no way Op1 could be found and not have a R1.

Post Reply

Return to “Ask For Help”