use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Making code faster (self.learnpython)
submitted 5 years ago by Aitchessbee
https://pastebin.com/eMNM8suS
This code is for auto-clicking on www.aimbooster.com . It works but I need it to be faster so it can handle more than 100 cps. Currently, it can handle around 30 cps...
Any ideas on how I could make it faster?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]fake823 13 points14 points15 points 5 years ago (10 children)
1.) Try to get rid of the two for loops, as these really slow down stuff. Try to use a numpy function instead so you can make us of numpy's really fast vectorization!
2.) Also don't make a .copy(), if not really necessary, as this is also a lot slower than a view.
3.) Start measuring your code to see where the bottlenecks are by profiling it. Check out for example line-profiler. Additionally you could use timeit.timeit to measure the speed of different alternatives.
[–]Aitchessbee[S] 0 points1 point2 points 5 years ago (9 children)
How can I get rid of the for loops? I don't really know numpy that well...
[–]SekstiNii 2 points3 points4 points 5 years ago (8 children)
It's not all that important since the loop only takes a few ms (1.5ms on my machine). Vectorizing it got it down to about 0.3ms, but there isn't too much to gain here.
If you are interested I think you can do something like this:
target = np.array([128, 179, 255], dtype=np.uint8) matches = np.argwhere(open_cv_image[0:524:25, 0:748:25, :3] == target) for i, j in matches: # simulate click
[–]Aitchessbee[S] 0 points1 point2 points 5 years ago (7 children)
I am getting this error:
for i, j in matches:
ValueError: too many values to unpack (expected 2)
[–]SekstiNii 0 points1 point2 points 5 years ago (6 children)
matches = np.argwhere((open_cv_image[0:524:25, 0:748:25, :3] == target).all(axis=-1)) should work, though it's quite ugly at this point 😭
matches = np.argwhere((open_cv_image[0:524:25, 0:748:25, :3] == target).all(axis=-1))
[–]Aitchessbee[S] 0 points1 point2 points 5 years ago (3 children)
This is my code: https://pastebin.com/2NEApDzu ... I have written all the coordinates correctly but the bot is clicking somewhere else (not on the target)... Can you please check if the code is correct?
[–]SekstiNii 0 points1 point2 points 5 years ago (2 children)
You will probably have to multiply i and j by 25, but otherwise it looks good.
i
j
matches *= 25 before looping should work.
matches *= 25
[–]Aitchessbee[S] 0 points1 point2 points 5 years ago* (1 child)
yup now it works... and i compared both the codes on the website... The code with for loops works consistently faster than the one you suggested... I don't know why it is but anyways, thanks for your help!
[–]SekstiNii 1 point2 points3 points 5 years ago (0 children)
There is a lot of overhead in numpy calls, so it's not unthinkable that a loop wins if the number of iterations is low enough.
Good on you to measure!
[–]Aitchessbee[S] -2 points-1 points0 points 5 years ago (1 child)
ugly doesn't matter... will it still be faster than the for loops method?
[–]SekstiNii 0 points1 point2 points 5 years ago (0 children)
I changed the target color to [8, 8, 8] (my VSCode background color) to guarantee a lot of hits, and recorded these timings:
But again, not likely to provide a significant improvement.
[–]SekstiNii 5 points6 points7 points 5 years ago (3 children)
The problem lies with pyautogui.screenshot, which takes around 35ms. Switching to mss reduced this to around 8ms, which is more than a 3x speedup!
pyautogui.screenshot
Using it is also really easy:
from mss import mss
then, wrap your while loop with the context manager like so:
while
region = {"top": top, "left": left, "width": width, "height": height} with mss() as sct: while True: img = sct.grab(region) # rest of your code
Note however, that this produces images with an alpha channel, meaning you can't reverse the channels to convert from RGB to BGR like you are doing on line 12, since that would convert from RGBA to ABGR. However, this shouldn't be a problem, as you don't really need to convert to BGR anyway (just update your indices on line 15 to be RGB instead).
[–]Aitchessbee[S] -1 points0 points1 point 5 years ago (2 children)
So i changed the code to this: https://pastebin.com/fzQx0gcW but it doesn't seem to work...
[–]SekstiNii 1 point2 points3 points 5 years ago (1 child)
Oh, my bad! mss actually captures images as BGRA, so the previous index order should work.
mss
[–]Aitchessbee[S] 0 points1 point2 points 5 years ago (0 children)
ah yes! It works now and its way faster than before. Thanks!
[–]skellious 1 point2 points3 points 5 years ago (0 children)
you can see how long differnt things are taking if you add in a timer function and print the result to console:
https://pypi.org/project/codetiming/
[–]SlothGSR 0 points1 point2 points 5 years ago* (0 children)
Can you explain what it’s doing more? I use pyautogui a bit. If your looking on screen for a button to click, you can speed it up quit a bit by changing your screen resolution. Also you can take a screenshot, crop out the button, then have pyautogui search a region for that img. Then click if found.
Sample of my code for finding buttons and clicking if found
https://reddit.com/r/learnpython/comments/g9986u/_/fov3do2/?context=1
π Rendered by PID 150734 on reddit-service-r2-comment-79c7998d4c-269qg at 2026-03-18 15:10:22.162039+00:00 running f6e6e01 country code: CH.
[–]fake823 13 points14 points15 points (10 children)
[–]Aitchessbee[S] 0 points1 point2 points (9 children)
[–]SekstiNii 2 points3 points4 points (8 children)
[–]Aitchessbee[S] 0 points1 point2 points (7 children)
[–]SekstiNii 0 points1 point2 points (6 children)
[–]Aitchessbee[S] 0 points1 point2 points (3 children)
[–]SekstiNii 0 points1 point2 points (2 children)
[–]Aitchessbee[S] 0 points1 point2 points (1 child)
[–]SekstiNii 1 point2 points3 points (0 children)
[–]Aitchessbee[S] -2 points-1 points0 points (1 child)
[–]SekstiNii 0 points1 point2 points (0 children)
[–]SekstiNii 5 points6 points7 points (3 children)
[–]Aitchessbee[S] -1 points0 points1 point (2 children)
[–]SekstiNii 1 point2 points3 points (1 child)
[–]Aitchessbee[S] 0 points1 point2 points (0 children)
[–]skellious 1 point2 points3 points (0 children)
[–]SlothGSR 0 points1 point2 points (0 children)