![]() |
| | #1 |
| Registered User Join Date: Jan 2008
Posts: 42
| Reduce CPU usage 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 | |
| | #3 |
| Registered User Join Date: Jan 2008
Posts: 42
| Do you mean it is only possible with sleep functions?! |
| patrick22 is offline | |
| | #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;
}
Code: #include <iostream>
#include <string>
#include <conio.h>
using namespace std;
int main()
{
while(!_kbhit());
return 0;
}
__________________ home page (new layout) |
| scwizzo is offline | |
| | #5 | |
| Registered User Join Date: Jan 2008
Posts: 42
| Quote:
| |
| patrick22 is offline | |
| | #6 |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 891
| 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 | |
| | #7 |
| and the hat of vanishing 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 | |
| | #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:
| |
| Elysia is offline | |
| | #9 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,378
| 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 | |
| | #10 |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,475
| |
| iMalc is offline | |
![]() |
| Tags |
| cpu, deleting files, overload, usage |
| Thread Tools | |
| Display Modes | |
|
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 |