SikulixIDE 1.1.2 - Would it be possible to optimize a long if/elsif/else loop with exists() inside?

Asked by PowerPenguin on 2018-03-17

Hello, I have a long piece of code and I was wondering if it would be possible to optimise it so it goes faster. I have to check certain region of the screen constantly to see what appears in it. The problem is that it can appear up to 200 different things, so it needs to compare 200 different images with whatever is in the screen until it founds the right one, and it has do to something different in the screen with each one of the possible options. Right now, it does that just fine, but if the image that appears is the last one of the loop it needs around 25 seconds to match it, and I would like to know if it would be possible to reduce that time.

This is my code:

r = regionOfTheScreen
while True:
if r.inside().exists((Pattern(img1).exact()),0):
    #do something
elif r.inside().exists((Pattern(img2).exact()),0):
    #do something else

+198 elifs more with the same form.

Could I change something to make it faster? I already run it with only the necessary programs running on my computer and I check the smallest possible region of the screen.

Thank you!

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Last query:
Last reply:
PowerPenguin (powerpenguin) said : #2

Hi, thank you for that. I haven't tried it, but now I have. I loaded the images inside an array, but I noticed with the debug on that it was using a lot of my cache and as a result my computer was very slow, not only when trying to figure out which image was on the screen, but also with the animations showing in the website or when writing something (it needed some seconds for the letter to start appearing). This is probably because they are a lot of images and my computer may not be able to work on that level. Is there anything else I could do?

I leave my code here, I renamed the images to p0, p1, p2 etc and the image it needs to clic after finding each result as r0, r1, r2.

r = regionOfTheScreen
images = [p0.png, p1.png, p2.png, etc]
while True:
    matches = a.findAnyList(images)
    for match in matches:

RaiMan (raimund-hocke) said : #3

Yes, searching for 198 images in parallel on the whole screen might lead to some memory constraints and cpu usage problems. but this only affects the cpu situation while the above snippet is running. If there are problems befor and /or afterwards, they have nothing to do with SikuliX.

findAnyList() is more effective than a series of exists() with more than 5 images.

... and no, there is no other solution than these two for your problem.

PowerPenguin (powerpenguin) said : #4

Alright, it's ok then, thank you very much for your help! I have one last question, but it's alright if I don't receive an answer on this. If I were to make this work, what kind of computer would I need? Can you guide me a bit on that topic? Thank you!

RaiMan (raimund-hocke) said : #5

depends on the system.

CPU: recent 2 or 4 core cpu beyond 2.5 GHz.

Linux systems should have at least 4GB storage.

For Windows at least 8GB are recommended (I have a windows 10 running on i7-2600 @ 3,4GHz, 16GB RAM)

My Mac systems run smoothly having 8GB+ (2,5+ GHz Intel Core i5)

Generally: the more physical RAM the better.

Can you help with this problem?

Provide an answer of your own, or ask PowerPenguin for more information if necessary.

To post a message you must log in.