Sign in to follow this  
Followers 0
PhilHibbs

Do-nothing loop

11 posts in this topic

#1 ·  Posted (edited)

I have a few scripts that just sit in the background and only do something when I press a key (I usually use ` for this). What's the best way to structure this kind of script? The simplest would be like this...

HotKeySet("`","Clicker")

While 1
WEnd

Func Clicker()
  MouseClick("left")
  MouseClick("left")
EndFunc

Is the empty While loop efficient? Is it going to run the CPU ragged, and if so is there a good way to make it less greedy?

Edited by PhilHibbs

Share this post


Link to post
Share on other sites



Extremely inefficient. You should be adding a sleep in there of at least 100.

By the way, a sleep time of 50 milliseconds is the minimum that AutoIt accepts.

i do sleep(10) and it seems to work o.O

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

i do sleep(10) and it seems to work o.O

I suspect that anything less than around 55 actually sleeps for 1/18.2 of a second, or more likely a random amount of time up to that depending on when the clock last ticked. Edited by PhilHibbs

Share this post


Link to post
Share on other sites

I suspect that anything less than around 55 actually sleeps for 1/18.2 of a second, or more likely a random amount of time up to that depending on when the clock last ticked.

i'm slightly interested in where the hell did you come up with the number

Share this post


Link to post
Share on other sites

By the way, a sleep time of 50 milliseconds is the minimum that AutoIt accepts.

Isn't it 10 ms?

$time = ""
$timer = TimerInit()
for $i = 1 to 10
    $time &= "Sleep " & $i & ": " & round(TimerDiff($timer)) & @crlf
    sleep(50)
Next
ConsoleWrite("sleep(50)" & @crlf & $time & @crlf & @crlf)

$time = ""
$timer = TimerInit()
for $i = 1 to 10
    $time &= "Sleep " & $i & ": " & round(TimerDiff($timer)) & @crlf
    sleep(10)
Next
ConsoleWrite("sleep(10)" & @crlf & $time & @crlf & @crlf)

$time = ""
$timer = TimerInit()
for $i = 1 to 10
    $time &= "Sleep " & $i & ": " & round(TimerDiff($timer)) & @crlf
    sleep(5)
Next
ConsoleWrite("sleep(5)" & @crlf & $time & @crlf & @crlf)

Share this post


Link to post
Share on other sites

i'm slightly interested in where the hell did you come up with the number

The old IBM PC clock ticked 18.2 times per second. I actually don't know if that is still true on modern hardware.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

No. 1-9 will be run as 10 link to thread about it

verified.

simple test:

$init = TimerInit() ; warming up timer
For $i = 0 To 20
    $init = TimerInit()
    Sleep($i)
    $diff = Round(TimerDiff($init))
    ConsoleWrite("Sleep("&$i&"); Actual sleep = "&$diff&@CRLF)
Next

Result:

Sleep(0); Actual sleep = 0

Sleep(1); Actual sleep = 10

Sleep(2); Actual sleep = 11

Sleep(3); Actual sleep = 11

Sleep(4); Actual sleep = 11

Sleep(5); Actual sleep = 11

Sleep(6); Actual sleep = 11

Sleep(7); Actual sleep = 11

Sleep(8); Actual sleep = 12

Sleep(9); Actual sleep = 14

Sleep(10); Actual sleep = 14

Sleep(11); Actual sleep = 12

Sleep(12); Actual sleep = 21

Sleep(13); Actual sleep = 22

Sleep(14); Actual sleep = 26

Sleep(15); Actual sleep = 21

Sleep(16); Actual sleep = 21

Sleep(17); Actual sleep = 26

Sleep(18); Actual sleep = 21

Sleep(19); Actual sleep = 21

Sleep(20); Actual sleep = 26

Edited by Mison

Hi ;)

Share this post


Link to post
Share on other sites

I suspect that anything less than around 55 actually sleeps for 1/18.2 of a second, or more likely a random amount of time up to that depending on when the clock last ticked.

To state the obvious.

Assuming sleep(1000) pauses the script execution for one second.

Then, the delay parameter units, as mentioned in the help file, is in milliseconds.

Therefore, sleep(55) should pause script execution for 55 milliseconds.

55 milliseconds = 55/1000 seconds = 1/18.18181818 which approx. equals 1/18.2 of a second.

PhilHibbs

If you do not know about how the sleep command deduces the cpu usage, then do the following test.

First - Open up the Task Manager. (Right click on desktop task bar and select from menu)

Select the Performance tab on Windows Task Manager window so you can see the CPU Usage display which should be on 0%.

When the script is running, notice the difference how not having a sleep inside the loop over works the CPU, compared to having a sleep inside the loop.

HotKeySet("`", "Clicker")
HotKeySet("{ESC}", "Terminate")

While 1
    sleep(10) ; Comment this line out to see difference in CPU Usage.
WEnd

Func Clicker()
    MouseClick("left")
    MouseClick("left")
EndFunc ;==>Clicker

Func Terminate()
    Exit 0
EndFunc ;==>Terminate

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0