views:

400

answers:

5

I've been working on win32, c,c++ for a while. I code on visual studio. Most of the time I see system idle process uses more cpu utilization. Is there a way to allocate more processor cycles to my program to run it faster? I understand there might be limitations from i/o, in those cases this question doesn't make any sense. OR did i misunderstood the task manager numbers? I'm in a confusion, please help me out. And I want to do something in program itself, btw I will be happy if answers are specific to windows.

Thanks in advance ~calvin

+1  A: 

The idle process only "runs" when no other process needs to. If you want to use more CPU cycles, then use them.

Alex
+3  A: 

If your program it the only program that has something to do (not wait for IO), its thread will always be assigned to a processor core.

However, if you have a multi-core processor, and a single-threaded program, the CPU usage of your process displayed in the task manager will always be limited by 100/Ncores.

For example, if you have a quad-core machine, your process will be at 25% (using one core), and the idle process at around 75%. You can only additional CPU power by dividing your tasks into chunks that can be worked on by separate threads which will then be run on the idle cores.

Timbo
Thanks for pulling me out of weeds...
calvin
+1  A: 

If your program is idling, it doesn't do anything, i.e. there is nothing that could be done any faster. So the CPU is probably not the bottle-neck in your case. Are you maybe waiting for data coming from the disk or network?

In case your processor has multiple cores and your program uses only one core to its full extent, making your program multi-threaded could work.

mxp
A: 

In a multitask / multithread OS the processor(s) time is splitted among threads.
If you want a specific thread to get bigger time chunk you can set its priority with the SetThreadPriority function, not wise to do it though.
Only special software (should) mess with those settings.

It's common for window applications to have a low cpu usage percent (which we see in the task manager)
because most of the time they just wait for messages.

Nick D
A: 

Use threads to:

  • abstract away all the I/O waits.
  • assign work to all cores.

also, remove all sleep-wait states from main thread.

Defer all I/O to a thread, so that wait states are confined within it. Keep the actual computations in the foreground thread, and use synchronization mechanisms that make the I/O slave thread to wait for your main thread when communicating.

If your CPU is multi-core, and your problem is paralellizable, create as many threads as you have cores, research "set affinity" functions to assign them between the cores and still keep a separate thread for all I/O.

Also pay attention not to wait in your main thread - usleep(1) doesn't send you into background for 1 microsecond, but for "no less than..." and that may mean anything between 1ms and 100ms but hardly ever less than that, and never anything close to a microsecond.

SF.