Reduce CPU usage

This is a discussion on Reduce CPU usage within the Windows Programming forums, part of the Platform Specific Boards category; Hi, i am deleting over 2000 files in a for loop, everything works ok except that the CPU usage is ...

  1. #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.

  2. #2
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Get a new CPU.
    MSDN <- Programmers Haven!

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    Do you mean it is only possible with sleep functions?!

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    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?

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    How long does it take to delete 2000+ files?
    it takes less than 6 secondes.

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    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)

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > 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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread


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