CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Reduce program's CPU usage

    I want to have a program running in the background that performs a lot of calculations. The problem is, this program takes up 99% of my CPU and makes all other programs run very slowly.

    I want to be able to have this program performing its calculations in the background and still be able to use other programs meanwhile, without the calculating program significantly affecting the other programs' performance. I don't mind if this means that the calculating program will run slower.

    Is there a way I can do this?
    Old Unix programmers never die, they just mv to /dev/null

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757

    Re: Reduce program's CPU usage

    Describe the calculation done in the worker thread. One possible solution is sleep. Is there an infinite loop?

    Kuphryn

  3. #3
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: Reduce program's CPU usage

    Lower the priority of your programm... Under Windows, look up SetPriorityClass and SetThreadPriority functions in MSDN.
    99% CPU usage itself isn't bad, if your process has idle priority and don't use too much memory, it wouldn't interfere other programms' performance.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  4. #4
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610

    Re: Reduce program's CPU usage

    If your task is to run in the background, it is defined as:

    1. A finite process
    2. Does not have a completion time frame

    Based on these two criteria, we can assume that speed of calculation is not important, because if your goal is a specific amount of CPU usage, you cannot aim for a specific effective speed of calculation.

    There is no good way of reserving CPU percent time on Windows, but the above advice of setting your process or thread priority to a lower grade will at best make Windows execute your thread only when no higher priority threads are active (not sleeping, waiting for mutexes, semaphores etc..). This is a very effective work strategy for background processes.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured