Pured

How does Sleep() work?

9 posts in this topic

#1 ·  Posted (edited)

Consider the following two scenarios:

While(1)
    Sleep(10)
WEnd

and

While(1)
    Sleep(100000)
WEnd

 

How does Sleep() work for the above two cases? Do they both use the same amount of cpu time?

I think the second scenario will check the time difference just as often as the first scenario, meaning the exact same amount of resources is spent in both scenarios. The only difference is that the second scenario waits longer before going to WEnd?

Is that correct? Or is doing Sleep(100000) better than doing Sleep(10)?

 

Extra question:

Which is more computationally efficient if I want an infinite loop running in the background?

The above question's answer or:

While(1)
WEnd

 

Thank you in advance.

Edited by Pured

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I don't know enough about the inner workings of AutoIt etc to definitively answer your question, however let's make it clear.

Sleep(10) which is a sleep of 10 milliseconds, does not equate to Sleep(100000) which is a sleep of 100,000 milliseconds, but I guess you are meaning repeating the Sleep(10) x 10,000 to match the 100,000.

So your question would be - Is doing Sleep(100000) better than doing Sleep(10) 10,000 times?
One answer of course, is that you really don't want to write out Sleep(10) the 10,000 times, so quite a saving can be made there, simply doing Sleep(100000). :lol:

My memory of the Help file etc, recommends doing a Sleep of at least 10 milliseconds to not overtax your CPU. So that answers your last question, and if you apply logic to that, it probably answers your first question too. In other words, if the system is checking every 1 millisecond anyway, then it is hard to see any benefit in the 10 millisecond sleep as being less taxing on your CPU.

I would wait for someone else here to give a definitive answer, but you can ponder on what I wrote until then. :D

Edited by TheSaint

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

@TheSaint, Thanks for the response.

I have read through that as well. The way I understood it was that a Sleep() under 10ms would be redundant as AutoIT itself checks for sleeps every 10ms. Also, the issue wasn't about writing out Sleep(10) X times. My assumption was that a Sleep(100) would do something like

$sleepStart = TimeInit()
While(($sleepStart - TimeDiff($sleepStart)) < 100)
    ;Do nothing...
WEnd

 

Share this post


Link to post
Share on other sites

Well, I suggest waiting for a definitive answer, and unless you are equating the Sleep(10) to Sleep(100000) in some way, it is not clear to me what you are asking in that regard, especially as I am not getting the sense of your While..Wend code .... you are doing Sleep for a specified amount of time, and nothing happens during that, so why not just use the total, as I see no benefit gained otherwise ... just a loss really, as other unnecessary queries/computations are occurring.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

@TheSaint, The Sleep needs to know when the Sleep is finished before it continues through the rest of the script. That in itself requires queries (if I'm not mistaken). I guess we'll have to wait for that definitive answer. :)

As for the Sleep(10) / Sleep(100000) thing, how I understand it, Sleep(100000) gets checked every 10ms, similar to a Sleep(10) or even Sleep(1). Therefore, the computation of the Sleep() may be greater than a While(1) loop, making Sleep(100000) worse than a looped Sleep(10) if not the same.

Share this post


Link to post
Share on other sites

Sleep(10000) should be less taxing on the CPU because if you use Sleep(10) in a while loop, AutoIt will have to check the loop every 10ms and thus consumes more resources. The Sleep function in AutoIt uses Window's Sleep function which I would guess is more optimized and does not check every millisecond. A little reading suggests that it uses the system ticks... I am not sure what that means by the way but it does look more promising.

Albeit, the difference would be too subtle and shouldn't make any real difference in this age of computers :lol:. Use what you feel comfortable with, I personally prefer Sleep(10)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

@TheDcoder, I'm not too sure about the computation behind system ticks, but I get how they work.

You say the difference shouldn't make a difference, which I agree for regular users (this was also mainly asked out of curiousity). However, I'm sure when I'm running 3 virtual machines, 2 games, a music player, youtube at 2x speed with multiple scripts which use Sleep() loops, it could make some difference.

Share this post


Link to post
Share on other sites
14 minutes ago, Pured said:

However, I'm sure when I'm running 3 virtual machines, 2 games, a music player, youtube at 2x speed with multiple scripts which use Sleep() loops, it could make some difference.

First off, why are you playing 2 games while watching a youtube at 2x? Seems like a good way to clog your brain :D

Anyway, it still shouldn't make a large difference. If you really want efficiency, use Sleep(10000000000) instead of Sleep(10).


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

I'm not surprised your PC needs more Sleep after doing all that lot at the same time. :muttley:

1 person likes this

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

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

  • Similar Content

    • K3STROS
      By K3STROS
      Self explanatory, why doesn't this work?
      Sleep((Random, 990, 1990, 1)) Thanks in advance