Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sleep function()

Status
Not open for further replies.
May 3, 2003
90
0
0
CA
Hello,

I have a simple question really. In one of my programs, I have a while loop. A fellow programmer recommended to sleep for 1 ms at the end of the loop to be cpu friendly. (it was at 100% before implementing it and reduced cpu usage to 0% on idle). My question : is there a way to sleep for less then 1 ms ? maybe a nano sec or something. No matter how complicated, I would like to know the answer.

Thank you
 
What kind of a program is it?

If it's performing some long and detailed calculation, then you may as well let it run at 100% until the job is completed.

But if it's something like say an editor which spends most of it's time waiting for user input, then there should be no reason for it to be using any CPU time until the user does something.

> A fellow programmer recommended to sleep for 1 ms at the end of the loop to be cpu friendly.
Sleep only guarantees a minimum time, not an absolute time. Typically, sleep times get rounded up to the default scheduling interval of the operating system (say 20mS).

If you're doing lots of calculations, but don't want to cripple the machine, a better way would be to drop the priority of the process. This ensures that maximum effort is spent doing calculations (so you're not waiting for the result), but also ensures that you retain responsive control of the machine should you need to do other things.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Well the program forwards udp packets and runs 24/7. I wanted to sleep for less then 1 ms to boost performance (not much but still, with huge amounts its noticeable).
Or if there is a better approach, please let me know but keep in mind performance is an issue and the network needs to run over 5000 frames/sec. The current code works well. Just wanted to know if you can sleep under 1 ms :)

Thank you very much.
 
You're doing something wrong if you're using 100% CPU to forward 5K packets per second.

It seems to me like you're using "busy-polling" to see if there are any messages to process, which uses 100% CPU, rather than using a "select-wait", which uses 0% CPU until there is something to be done.

Then there is also I/O Completion


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Try Windows API call Sleep(0) to relinquish the remainder of the current thread time slice to any other thread of equal priority.
Windows is not real-time OS. IMHO, Windows sheduler can't reasonably maintain less than 1 ms sleep request.
 
I have tried the sleep(0). But this completely bypasses the sleep and still uses 100%. Even though with the performance count gives better results :

Result for sleep 0 : 16326 tick count (average on 5 tests)
Result for sleep 1 : 3860460 tick count (average on 5 tests)

Salem, thank you for the info but I have used a different approach and would require time i dont have to replace the code :(. I will keep this info in mind for future projects.

Thank you very much, all the help was much appreciated
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top