Processor Spike

Started by
4 comments, last by Andrew Russell 18 years, 10 months ago
I made a console program in C++ for Windows XP, it's a script engine that reads some simple instructions from a file and executes them in order inside of a loop. When the program runs, though, my entire computer begins to freeze up and when I ALT-TAB to check my Processes, it shows my program hogging 99% of the CPU. The program doesn't need that much CPU to run, how can I somehow get my program to slow down, or to cap how much of the CPU it uses? I want my script engine to run in the background while I use my computer for other stuff. Thanks.
Advertisement
Put a small sleep function at the end of the "loop".

if your in windows...
include windows.h

Sleep(time_in_milliseconds);
Have you timed individual portions of your code? Maybe there's an algorithm in there that could use an optimization, one example could be a loop within a loop, within a loop, which could produce some horrendously inefficient code. Is your script file particularly large? If it's quite large the computer might be saving it to virtual memory which would be a serious CPU killer. Then there's the possibility that the compiler is doing it, try compiling in Release mode and see if that doesn't help. Also keep in mind that the student versions of MSVS .Net don't have an optimizing compiler, and I find my version hangs a little bit at the beginning of a compilation. If all of these issues have been addressed I would say that a quick and dirty solution would be to run the engine in it's own thread and call a sleep function from time to time.

(also keep in mind if your running programs like Google Compute, your processor will always be at ~99%)
"Think you Disco Duck, think!" Professor Farnsworth
Quote:Original post by FlyingDemon
Put a small sleep function at the end of the "loop".

if your in windows...
include windows.h

Sleep(time_in_milliseconds);

This generally works quite nicely. But it can often leave the CPU not doing anything useful at all. It's a good simple and effective option, though and I use it myself sometimes.

The other option you have is to run your program at a lower priority. That way it can use 100% CPU if available, but it will otherwise scale back to lower usage more readily when other programs want to use the CPU.
I think that Sleep method is best, myself.
Running a loop checking for something usually wont hurt if it checks only 100 - 1000 times per second as opposed to millions or billions of times per second. Nobody is going to notice if your computer takes 5ms to react to a keypress.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Actually, that depends on the situation. If the loop is just polling, yes - then Sleep is the best method. If the loop is just processing stuff, then a change in priority is a better solution.

I was assuming that it was continuiously processing, rather than polling.

This topic is closed to new replies.

Advertisement