C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-05-2009, 01:14 PM   #1
Registered User
 
Join Date: Jan 2008
Posts: 42
Reduce CPU usage

Hi,

i am deleting over 2000 files in a for loop, everything works ok except that the CPU usage is 100 % during this loop. Here is the Code:

Code:
for( size_t i = 0; i < filesO.GetCount(); i++ ) 
{
	DeleteFile( filesO.Item( i ) );
}

How can i prevent increased CPU usage? i have seen programs doing the same thing but the CPU usage remains under 20%.

Is it there a way to do that without using sleep functions?



Thanks in advance.
patrick22 is offline   Reply With Quote
Old 07-05-2009, 01:38 PM   #2
Student
 
legit's Avatar
 
Join Date: Aug 2008
Location: UK -> Newcastle
Posts: 156
Get a new CPU.
__________________
MSDN <- Programmers Haven!
legit is offline   Reply With Quote
Old 07-05-2009, 01:56 PM   #3
Registered User
 
Join Date: Jan 2008
Posts: 42
Do you mean it is only possible with sleep functions?!
patrick22 is offline   Reply With Quote
Old 07-05-2009, 08:21 PM   #4
Registered User
 
Join Date: Mar 2007
Posts: 333
Loops eat CPU power period. The power consumed is somewhat based off what is inside the loop. Take the following code for example. It pegs out my CPU at 50% (100% on one of two cores).

Code:
#include <iostream> 
#include <string>
using namespace std;

int main()
{
    int temp = 999999999;
    for(int i = 0; i < temp; i++);
    return 0;	
}
Then take the other piece of code for example. This does 20% of one core.
Code:
#include <iostream> 
#include <string>
#include <conio.h>
using namespace std;

int main()
{    
    while(!_kbhit());
    return 0;	
}
Hopefully this gives you some insight in to reducing the CPU usage, however, Sleep() may be your only choice. How long does it take to delete 2000+ files?
__________________
home page (new layout)
scwizzo is offline   Reply With Quote
Old 07-05-2009, 08:28 PM   #5
Registered User
 
Join Date: Jan 2008
Posts: 42
Quote:
How long does it take to delete 2000+ files?
it takes less than 6 secondes.
patrick22 is offline   Reply With Quote
Old 07-05-2009, 08:59 PM   #6
int x = *((int *) NULL);
 
Cactus_Hugger's Avatar
 
Join Date: Jul 2003
Location: Banks of the River Styx
Posts: 891
Quote:
Originally Posted by patrick22 View Post
it takes less than 6 secondes.
Why is this a problem? You're asking the computer to do something, but you dislike the fact that it is doing it. You're deleting 2000 files -- I would have expected such an operation to be bottlenecked by disk IO, but perhaps it is getting buffered somewhere.

Certainly you could reduce CPU usage by inserting calls to Sleep(), but then the operation would take longer to complete. Just do what you need to do, as fast as you can. Reduce CPU with better algorithms or not spinning your wheels waiting for something to happen (neither of which seem possible here). Asking the computer to do something and asking it to not use CPU power are mutually exclusive. Reduce the amount of work you have to do in the first place, and you'll reap the resource gains there.
__________________
long time; /* know C? */
Unprecedented performance: Nothing ever ran this slow before.
Any sufficiently advanced bug is indistinguishable from a feature.
Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
The best way to accelerate an IBM is at 9.8 m/s/s.
recursion (re - cur' - zhun) n. 1. (see recursion)
Cactus_Hugger is offline   Reply With Quote
Old 07-05-2009, 10:23 PM   #7
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> i have seen programs doing the same thing but the CPU usage remains under 20%.
Do they also take 6 seconds?
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 07-06-2009, 05:50 AM   #8
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
You can make a program that deletes the files in 6 seconds with 100% cpu use...
Or you can make a program that takes 30 seconds with 20% cpu use...
Your choice.
You can't have one and the other.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-08-2009, 09:39 PM   #9
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,378
Quote:
Originally Posted by Elysia View Post
You can make a program that deletes the files in 6 seconds with 100% cpu use...
Or you can make a program that takes 30 seconds with 20% cpu use...
Your choice.
You can't have one and the other.
Deleting files is an entirely I/O bound operation. It should NEVER consume 100% of the CPU. Something is wrong somewhere.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is online now   Reply With Quote
Old 07-10-2009, 02:13 PM   #10
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,475
Quote:
Originally Posted by brewbuck View Post
Deleting files is an entirely I/O bound operation. It should NEVER consume 100% of the CPU. Something is wrong somewhere.
Yeah that's what I would think too.
Or perhaps some kind of "Software RAID" is in use?
iMalc is offline   Reply With Quote
Reply

Tags
cpu, deleting files, overload, usage

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
questions on multiple thread programming lehe C Programming 11 03-27-2009 07:44 AM
Net cpu usage of pthreads?! mynickmynick C++ Programming 15 09-26-2008 07:59 AM
Calculating CPU Usage vitaliy Linux Programming 3 08-21-2005 09:38 AM
Win2K Limiting CPU Usage? drdroid Tech Board 4 03-31-2004 02:08 PM
CPU Usage so high X PaYnE X Windows Programming 9 12-21-2003 03:07 AM


All times are GMT -6. The time now is 05:01 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22