1

im currently learning Python(about 3 months expirience now) and wanted to try out the module "Mouse", ive built a simple autoclicker script(not using it in game) but for values above 12(cps) it reaches below targeted cps, i suspect this is because of the if loops in my program, could anybody help me make this more efficient?

Code here:

import mouse
import keyboard
import time

staat = 0
cps = input("Typ de hoeveelheid cps die je wilt in.")
cps = float(cps)
cps = 1 / cps
print("pauzeer de loop door op / te drukken")
while True:
    if staat == 1:
        mouse.click('left')
    if keyboard.is_pressed('/'):
        if staat == 0:
            staat = 1
        else:
            staat = 0
    time.sleep(cps)

Thanks in advance

  • 2
    The first thing I can think of is that your `mouse.click()` code and the `keyboard.is_pressed()` code can run simultaneously, they don't have to be in the same loop. I think [asyncio](https://docs.python.org/3/library/asyncio.html) seems like the right tool for that. But it could be that Python just has too much overhead for the tight loops you need; it could be that you'd need native code to achieve over a certain amount of speed, or at least use something like Cython. – Random Davis Sep 24 '21 at 20:08

1 Answers1

0

Using 1 instead of True is slightly faster. Importing this way is slightly faster and calling without the '.' is also slightly faster. If still not fast enough I could implement multi-threading .

from mouse import click
from keyboard import is_pressed
from time import sleep,perf_counter

staat = 0
cps = input("Typ de hoeveelheid cps die je wilt in.")
cps = float(cps)
cps = 1 / cps
print("pauzeer de loop door op / te drukken")
while 1:
    timeing = perf_counter()
    if staat:
        click('left')
    if is_pressed('/'):
        if not staat:
            staat = 1
        else:
            staat = 0
    sleep(cps - (perf_counter() - timeing)
R0Best
  • 41
  • 6
  • I am also quite new to this, trying to help. Please tell me if it's still not fast enough or if it doesn't work properly. Also can I can know what your target CPS is? – R0Best Sep 24 '21 at 20:42