Speed up while loop?

Asked by Wing Chi Samuel Wong

my script
it scrolls down 1 step 1 second
even i put MoveMouseDelay =0.2
I guess its not because of the mousemove
its the script itself need to run faster

wait (3)
Settings.MoveMouseDelay = 0.2
reg = Pattern("ETASbyCountr.png").similar(0.90).targetOffset(0,1)
steps = 1
while not exists(reg, 0):
     wheel(Location(416,422), WHEEL_DOWN, steps)

is there a way to make this script run faster?
thank you very much.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
j
Solved:
Last query:
Last reply:
Revision history for this message
Best j (j-the-k) said :
#1

The most time is spent when the image is searched in the "exists" command.
So if "ETASbyCountr.png" doesn't move on the screen, you could use a smaller region to search for the reg pattern:

searchRegion = Region.create(x,y,w,h,) # create region containing your image
while not searchRegion.exists(reg, 0):
    wheel(Location(416,422), WHEEL_DOWN, steps)

Now Sikuli will search for the image inside that region instead of the whole screen.

Revision history for this message
RaiMan (raimund-hocke) said :
#2

@ j-the-k
In the current Sikuli, on the Jython layer to create a region use:
searchRegion = Region(x,y,w,h)

Region.create() should only be used on the Java layer.

This is because there currently is a weird difference between regions in the Jython API and the Java API regarding observe(). This might be fixed in the next version.

Revision history for this message
Wing Chi Samuel Wong (wsam7) said :
#3

ok i guess that as well, but i cant really fix the region. i use the same script on different machine and also they got smaller and larger monitor with different resolution
so location of what i am looking for will be in different area.

i look at region before but dont think i can set that.

anyway i will stick to the current script unless i come across a way to fix where that pattern appear.

thx alot

ps. so the while loop itself is fast enough, just the pattern matching taking times

Revision history for this message
Wing Chi Samuel Wong (wsam7) said :
#4

Thanks j, that solved my question.

Revision history for this message
RaiMan (raimund-hocke) said :
#5

You can define regions relative to some fixed aspects of your application (e.g. a logo or a title bar) using the many possibilities to define Regions dynamically.

# the basic approach
ref = find("logo.png")
reg = Region(ref.x+300, ref.y+200, 400, 100)

and many other possibilities.
Measuring pixel aspects: see faq 1686

this works independently of the resolution, as long as the pixels that make up your image to search stay the same.