Python Forum
simple code is way too slow - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: simple code is way too slow (/thread-14009.html)



simple code is way too slow - JAREDZ - Nov-11-2018

hello, i'm new to python and i tried this simple code to accomplish actions when a pixel has the expected color, but it's so slow i don't understand why, it seems like it takes way too long to just check pixel color.

#python3

import pyautogui
while not pyautogui.pixelMatchesColor(1006, 323, (253, 253, 253), tolerance=2) :
		pyautogui=pyautogui
pyautogui.click(1010, 315, button='left')
pyautogui.click(685, 437, button='left')
pyautogui.hotkey('tab', 'enter') 
pyautogui.click(715, 467, button='left')
pyautogui.click(715, 467, button='left')
pyautogui.click(905, 245, button='left')
pyautogui.hotkey('esc') 
i just want my code to wait until the pixel at the given location is 253, 253, 253, with a tolerance of 2, and then, right away, it should start clicking, etc, but it has a long delay, idk why.
can you help me ? what's wrong?


RE: simple code is way too slow - Larz60+ - Nov-11-2018

run with a profiler, see: https://docs.python.org/3/library/profile.html


RE: simple code is way too slow - JAREDZ - Nov-11-2018

(Nov-11-2018, 09:43 AM)Larz60+ Wrote: run with a profiler, see: https://docs.python.org/3/library/profile.html

thank you for your answer, I tried to run with, python3 -m cProfile myscript.py
i don't see any difference tbh, still almost 2 seconds delay before actions, when the pixel has the right color, shouldn't be so long, what am I doing wrong ?


RE: simple code is way too slow - Larz60+ - Nov-11-2018

running profile will show you where the bottleneck is, it doesn't fix it for you.

show report, should look something like:
Output:
197 function calls (192 primitive calls) in 0.002 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.001 0.001 <string>:1(<module>) 1 0.000 0.000 0.001 0.001 re.py:212(compile) 1 0.000 0.000 0.001 0.001 re.py:268(_compile) 1 0.000 0.000 0.000 0.000 sre_compile.py:172(_compile_charset) 1 0.000 0.000 0.000 0.000 sre_compile.py:201(_optimize_charset) 4 0.000 0.000 0.000 0.000 sre_compile.py:25(_identityfunction) 3/1 0.000 0.000 0.000 0.000 sre_compile.py:33(_compile)



RE: simple code is way too slow - JAREDZ - Nov-11-2018

(Nov-11-2018, 10:20 AM)Larz60+ Wrote: running profile will show you where the bottleneck is, it doesn't fix it for you.

show report, should look something like:
Output:
197 function calls (192 primitive calls) in 0.002 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.001 0.001 <string>:1(<module>) 1 0.000 0.000 0.001 0.001 re.py:212(compile) 1 0.000 0.000 0.001 0.001 re.py:268(_compile) 1 0.000 0.000 0.000 0.000 sre_compile.py:172(_compile_charset) 1 0.000 0.000 0.000 0.000 sre_compile.py:201(_optimize_charset) 4 0.000 0.000 0.000 0.000 sre_compile.py:25(_identityfunction) 3/1 0.000 0.000 0.000 0.000 sre_compile.py:33(_compile)

wow, that's what i get, so weird, why so many function calls for such a little scrit



RE: simple code is way too slow - Larz60+ - Nov-11-2018

That's too much output for a single post. look for repetition of same sequence of code.
i suspect you should:
review parameters to pyautogui
from the docs:
Quote:Like the enchanted brooms from the Sorcerer’s Apprentice programmed to keep filling (and then overfilling) the bath with water, your program could get out of control (even though it is following your instructions) and need to be stopped. This can be difficult to do if the mouse is moving around on its own, preventing you from clicking on the program’s window to close it down.

As a safety feature, a fail-safe feature is enabled by default. When pyautogui.FAILSAFE = True PyAutoGUI functions will raise a pyautogui.FailSafeException if the mouse cursor is in the upper left corner of the screen. If you lose control and need to stop the current PyAutoGUI function, keep moving the mouse cursor up and to the left. To disable this feature, set FAILSAFE to False:



RE: simple code is way too slow - JAREDZ - Nov-11-2018

(Nov-11-2018, 10:34 AM)Larz60+ Wrote: That's too much output for a single post. look for repetition of same sequence of code.
i suspect you should:
review parameters to pyautogui
from the docs:
Quote:Like the enchanted brooms from the Sorcerer’s Apprentice programmed to keep filling (and then overfilling) the bath with water, your program could get out of control (even though it is following your instructions) and need to be stopped. This can be difficult to do if the mouse is moving around on its own, preventing you from clicking on the program’s window to close it down.

As a safety feature, a fail-safe feature is enabled by default. When pyautogui.FAILSAFE = True PyAutoGUI functions will raise a pyautogui.FailSafeException if the mouse cursor is in the upper left corner of the screen. If you lose control and need to stop the current PyAutoGUI function, keep moving the mouse cursor up and to the left. To disable this feature, set FAILSAFE to False:

I've added the failsafe. my script is actually a loop, when it finds the pixel, it does the actions, then it looks for the same pixel again, etc. I've changed it so i can see the output, and made it so it stops when it finds the pixel once.
I don't understand what you mean when you say 'review parameters to pyautogui', sorry i'm really noob with python, there shouldn't be so many function calls, right ? maybe i should try something else than pyautogui for my purpose ? I don't know what to do =/


RE: simple code is way too slow - Larz60+ - Nov-11-2018

the proper word is attributes, (which are parameters)

func(item1, item2)
item1 and item2 are attributes of func

example real usage: https://www.thetaranights.com/gui-automation-with-python-example-usage-of-pyautogui/
https://automatetheboringstuff.com/chapter18/