Hi!
If I delay my program for certain amount of milliseconds, how much CPU is used?
Hi!
If I delay my program for certain amount of milliseconds, how much CPU is used?
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
Depends on what you use. If you use Sleep() I believe it doesn't waste CPU cycles(This is for windows). If you use a standard solution it will basically put your program in a loop which will eat some cycles. This would be one of those times where system dependent is better than standard.
Woop?
Sleep() makes the CPU time avaliable to other applications.
If your program is looping for a long time, you should include Sleep() unless your application really needs to utilize all of the CPU power available. I've used a few applications that allow the user to select "CPU priority" or "CPU utilization". I assume this allows me to change the Sleep() time.
It is bad practice (with a multitasking OS) to hog the CPU if your program is running a loop that's not doing anything useful while you wait for something else to happen.
Your CPU is actually running 100% all the time... But during the sleep-time, your code is not executing.
If Windows says that your CPU usage is 5%, then 95% of the cycles are being "wasted" by the operating system running a do-nothing loop... waiting for something exciting to do!
heh, the OS will not just keep your application using the CPU exclusively until you sleep. If another process (for instance, explorer.exe) needs CPU time, it will use context switching and allow the other process some cycles and then return to yours. Don't get the impression that you have complete control of the CPU at any time. If you don't need to loop constantly, then don't, if you do, then let your process do the work and if it's not as high a priority, then use the API for handling thread priorities.
Rockytriton is right! The operating system will allocate time to ALL running applications that request it. With Windows, the only way to "take over" the CPU, is with a kernel-mode program (driver).
But, the operating system doesn't know if your program is really doing anything useful, or just running a "useless" loop. So, it can slow-down other applications... Well, it can slow-down other applications even if it's running a useful loop.
Wow! Rockytriton, you're really in Antarctica? That's coo.... umm.. I mean awesome!
I would suggest if you aren't doing something useful in a loop that instead of calling Sleep(), you call break;
You could set any process to REALTIME_PRIORITY_CLASS with SetPriorityClass and your process would get higher priority then the OS but that would't be a good idea.Originally Posted by DougDbug
Sleep() isn't accurate. The millisecond value passed to it should just be seen as a sugested sleep time.
Last edited by Quantum1024; 10-27-2005 at 09:58 PM.