dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: how to reduce CPU Usage

  1. #1
    Join Date
    Oct 2003
    Location
    bangalore
    Posts
    33

    how to reduce CPU Usage

    hi,

    in my program there is continues while loop, becouse of this CPU usage is 100% , to reduce the CPU usage what should be done.

    i am calling System.Windows.Forms.Application.DoEvents() its of no use.

    i am also using Sleep(), imported from kernel32.dll

    with sleep it is just reducing the CPU usage from 100% to 95%, it is slowing down the process.


    Regards,
    Omprakash.

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: how to reduce CPU Usage

    Without seeing any code it is hard to tell. What is your loop doing? Can you put this in another thread? Are you waiting for something to happen? If so, could you use an event instead of a wait loop?

    I'm just throwing out a few ideas because we don't know what your loop is doing.

  3. #3
    Join Date
    Jan 2006
    Posts
    293

    Re: how to reduce CPU Usage

    Of course it is going to be at 100 percent while it is in the while loop... it is processing and running the application. The only way to stop it is to stop the looping, which you don't want to do because you want your program to run and process. If it is a long loop, then you can see about putting it in a thread, perhaps a low priority thread if you are worried about it bogging the system down. Regardless of if it is in a low priority thread or not, it will take the idle cpu cycles and still be at 100 percent, although the other apps can also be responsive if their thread priorities are above it. Trying to "slow" the process down in order to reduce CPU cycles (like your sleeping) is counter productive, and not necessary...
    Last edited by gigemboy; December 29th, 2006 at 03:36 PM.

  4. #4
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: how to reduce CPU Usage

    If you are going to use, or try a Sleep, use:
    Code:
    System.Threading.Thread.Sleep(0)
    'Which yields execution to other threads
    Then your process will be more yielding to other processes, that need the CPU. Check the CPU usage, under a heavy load.
    You could also drop to an idle/lower priority during the loop.


    Try to use Sendkeys.Flush() instead of Application.DoEvents().

  5. #5
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: how to reduce CPU Usage

    This should help your needs


    Code:
          ' Flush/DoEvents every nth iteration.
    
            Dim i As Integer
            Dim n As Integer = 2 'modify the 2, to fit your needs
            For i = 0 To 10
                If i Mod n = 0 Then  
                    SendKeys.Flush()
                End If
                System.Threading.Thread.Sleep(0)
    
            'Your loop stuff here
    
            Next

  6. #6
    Join Date
    Dec 2006
    Posts
    86

    Arrow Re: how to reduce CPU Usage

    I'm glad someone brought up this question - I have been wondering how to do it, too. Here's an example for why you would use a continuous loop but not use CPU much:
    The mouse pointer, as far as I know, is (or should be) an infinite loop, which only stops if a flag is set to true. This is low-level in the system, and does not concern us, but it shows one thing: that an infinite loop does not have to use 100% of the CPU. And why is that a concern? If you have a desktop, it isn't. If you have a laptop, you don't want heat produced for every single continuous loop you create - it would slow the system too much. The mouse should even work with 1% CPU usage - or probably less, because it doesn't do much. So although by reducing CPU usage, you "slow" the process, it is not necessarily bad, because repainting the cursor 10,000 per second is not helpful unless your monitor has a 10,000 Hz refresh rate - and your eyes can detect it. So I would really like an answer to this question, and I have rephared it for clarity (hopefully):
    Is there a way to make your program think that the processor is operating at a lower speed? Thanks.

    *Edit:
    We all know (or should know) about the DoEvents() method, and we all know that it does not help the CPU usage - so calling it significantly lowers performance, if you have no events to process. That basic for loop for the DoEvents does not do our job.
    Last edited by wfunction; January 3rd, 2007 at 12:43 PM. Reason: Forgot to add something

  7. #7
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: how to reduce CPU Usage

    Quote Originally Posted by wfunction
    The mouse pointer, as far as I know, is (or should be) an infinite loop, which only stops if a flag is set to true. This is low-level in the system, and does not concern us, but it shows one thing: that an infinite loop does not have to use 100% of the CPU.
    Hmm Sorry to burst your bubble here.. but the mouse (and keyboard for that matter) is interupt driven. If you press a key or move the mouse (Serial, PS2 or USB) a hardware interupt is raised, and the system then responds to it..

    If we look as a PS2 mouse: While the mouse is stationary, no data is sent.. so no CPU usage .. If you move the mouse.. data is then sent to the PS2 port.. The Port controller raises a hardware interupt (Normally - 12).. the cpu then (if it's not too busy) will pass the data over to the Windows subsystem which will update the pointer position.. and then clear the Interupt..

    Gremmy...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  8. #8
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: how to reduce CPU Usage

    I still dont know why someone wants to put a mouse click into an infinite loop. The example was a joke, that nobody gets, and now want to reproduce.

    However this is a good point:
    If you have a laptop, you don't want heat produced for every single continuous loop you create Is there a way to make your program think that the processor is operating at a lower speed? Thanks.-
    This is one of the reasons that I'm still working on a "process throttle" function. So you can set the max percentage, or proportion of the total.

    Another way if you're using 2005, is to add a performance counter, so that you can obtain CPU usage of a process. Here is a thread about that:
    GetCPUUsageThen depending on the usage just modify "i mod n", and "Sleep" in the example here, to be a longer duration, ie use a variable Sleep(V)

    *Edit:
    We all know (or should know) about the DoEvents() method, and we all know that it does not help the CPU usage - so calling it significantly lowers performance, if you have no events to process. That basic for loop for the DoEvents does not do our job
    At face value, that's correct, but you can still use it in several ways without that much cost.

  9. #9
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: how to reduce CPU Usage

    Quote Originally Posted by omrao
    in my program there is continues while loop, becouse of this CPU usage is 100% , to reduce the CPU usage what should be done.

    i am calling System.Windows.Forms.Application.DoEvents() its of no use.

    i am also using Sleep(), imported from kernel32.dll

    with sleep it is just reducing the CPU usage from 100% to 95%, it is slowing down the process.
    When any code is executed the CPU usage will imidiately jump to 100% (even if only for a split second..) remember task manager only shows Average CPU usage for a timed period (Close to 1 second, but not exact: Experimentation put it at almost 1.25 seconds)

    When there is code to execute the CPU will Work At 100% every time- NO EXCEPTIONS.. (unlike some employees that i used to have ) Programmers can use timers, Sleep commands, Doevent, and interupts to release the CPU time to other applications, But the majority of the time you WANT 100% of the CPU to finish your process as soon as possible..

    Ok so when dont you want 100%...

    1: To incorperate a cancle button (Use Doevents)
    2: To monitor Activity of something (for hardware use the Interupt. For software use a Timer or subclassing)
    3: To follow some real time timing (Use a timer or Sleep command)

    So now .. Why is there a issue of 100% CPU usage, why do you need to bring it down.. how long will this loop run ??

    Gremmy....
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  10. #10
    Join Date
    Dec 2006
    Posts
    86

    Re: how to reduce CPU Usage

    Wow - so, OK, they don't work with polling.
    However, what if you have to poll, for some reason?
    If you notice, some things just do not work with interrupts - I'll give an example when I remember.
    Oh, here's one, although it has no burden on the CPU:
    How does an app know when 1 second has passed? The OS lets it know... but how does the OS know? The BIOS lets it know... but how does the BIOS know? There has to be an infinite loop somewhere in the computer. Although it isn't a good example, there has to be a way to make a thread think it's on a 100 MHz computer, even if it isn't. Worst-case scenario: polling isn't the best thing, but it can be a lot easier than interrupt.
    So, does anyone have a solution?

  11. #11
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: how to reduce CPU Usage

    GremlinSA,

    Thanks for contributing to the thread!

    remember task manager only shows Average CPU usage for a timed period (Close to 1 second, but not exact: Experimentation put it at almost 1.25 seconds)
    That depends on the setting found in it's menu.


    So now .. Why is there a issue of 100% CPU usage, why do you need to bring it down.. how long will this loop run ??
    This is a problem for us prime number hunters, using laptops.
    Prime95, RMA, NewPGen, and LLR, all will 100% of the CPU, even on the most idle priority. Laptops are getting better at the cooling, but the new ones are slower at number crunching.

    Prime95.exe is an important program, for stress testing your computer.
    It can help find hardware failures, or incorrect software configurations.

  12. #12
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: how to reduce CPU Usage

    The internal clock of a PC is on a seperate Proccessor of its own, (Why the clock keep running even when the PC in unplugged..) and most Timed functions run off this Seperate processor..

    Gremmy...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  13. #13
    Join Date
    Jan 2006
    Posts
    293

  14. #14
    Join Date
    Oct 2003
    Location
    bangalore
    Posts
    33

    Re: how to reduce CPU Usage

    Hi all,

    Thanks for the reply,

    i tried using thread and timer, but it d'nt help, only thing works is Sleep() function, it slows down the process...

    Thanks,
    Om

  15. #15
    Join Date
    Jan 2006
    Posts
    293

    Re: how to reduce CPU Usage

    I just don't understand why the slowing down is necessary. The more you slow it down the longer the process will take to finish, which means the longer the user will have to wait to get the information that is processing, when it could have been completed quicker without slowing it down. 100 percent usage of the CPU is not a problem when you put the code that is looping in its own thread at a lower priority.

    I have an UnJumble program that finds all combinations of words and letters (and anagrams) with whatever letters are entered in a box. Depending on the number of letters, the process can take several hours to complete. The looping runs the entire time while it is processing for hours, so I put it on its own thread at a lower priority. The entire time the process is running, I can still use all of my programs without hiccups. There is no slowdown on my system (even though the processor usage is always at 100 percent during it), because all of the other processes are at a higher priority, which interrupts this lower priority thread that is doing the processing to do what the higher priority processes need to do.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)