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!

How to prevent big loops from showing 100% CPU

Status
Not open for further replies.

SuperHebbe

Programmer
Dec 25, 2005
6
0
0
NO
Hi

In my stats program I have a a lot of big loops to process some log files, and each time the CPU goes all the way up to 100% even I had set priority to the lowest (IDLE_PRIORITY_CLASS)

I tried this code to prevent the CPU to go so high:
Code:
UBYTE sleep_counter = 0;

for (i = 0; i < size; i++)
{
  if (sleep_counter == 200)
  {
    sleep (1);
    sleep_counter = 0;
  }
  DoStuff here...
}

It works, but slows down the program...
Is there another way to do this?

PS! I can't see that this 100% CPU peak does anything to other programs on my computer, but other users "think" it's does that because they are seeing the 100% CPU peak.



Thanks

Jan Erik
 
There's a difference between CPU usage, and process priority.

If there is absolutely nothing else going on, then you want as much CPU time dedicated to your background task. Otherwise, you end up with "It works, but slows down the program..."

> PS! I can't see that this 100% CPU peak does anything to other programs on my computer,
That would be my impression as well, if this work is being performed at IDLE_PRIORITY_CLASS. Any bit of work instigated by the user is going to pre-empt that and be finished before the background work resumes.

> but other users "think" it's does that because they are seeing the 100% CPU peak.
So give them a check box to play with, and let them figure out whether faster results or a flat-line process monitor is more important to them.
The whole point of having a multi-GHz machine is to open the throttle and get something done in a finite amount of time, not endlessly surf the web at 1% CPU.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top