Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simple code is way too slow
#1
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?
Reply
#2
run with a profiler, see: https://docs.python.org/3/library/profile.html
Reply
#3
(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 ?
Reply
#4
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)
Reply
#5
(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
Reply
#6
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:
Reply
#7
(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 =/
Reply
#8
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-automa...pyautogui/
https://automatetheboringstuff.com/chapter18/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simple code JacobSkinner 1 229 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 440 Nov-07-2023, 04:32 PM
Last Post: snippsat
  help me simple code result min and max number abrahimusmaximus 2 870 Nov-12-2022, 07:52 AM
Last Post: buran
  Simple encoding code ebolisa 3 1,400 Jun-18-2022, 10:59 AM
Last Post: deanhystad
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,755 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  My python code is running very slow on millions of records shantanu97 7 2,515 Dec-28-2021, 11:02 AM
Last Post: Larz60+
  Simple code question about lambda and tuples JasPyt 7 3,238 Oct-04-2021, 05:18 PM
Last Post: snippsat
  My simple code don't works !! Nabi666 1 1,577 Sep-06-2021, 12:10 PM
Last Post: jefsummers
  Optmized way to rewrite this very slow code liva28 0 1,472 Jul-18-2021, 12:16 PM
Last Post: liva28
Sad SyntaxError: from simple python example file from mind-monitor code (muse 2) warmcupoftea 4 2,752 Jul-16-2021, 02:51 PM
Last Post: warmcupoftea

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020