Jump to content
  • Advertisement
Sign in to follow this  
Kranar

Processor Spike

This topic is 4751 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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%)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!