Reducing CPU Usage in Looping

New to FreeBASIC? Post your questions here.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Reducing CPU Usage in Looping

Postby datwill310 » Apr 06, 2017 15:47

As you may or may not know, I am programming a game. In the main menu, I am using a do loop which contains:
- Redrawing the screen: this only occurs once a certain redraw flag = true.
- Checking for mouse input using a custom function (which is pretty minimalist: only a few lines).
- Acting on mouse input: manipulation of a few variables to ensure that buttons are pressed and something happens.
Although there is quite a bit of code, most of it is hardly run because the redrawing process happens only every time the user clicks on a button.
What I'm worried about is CPU usage in this main menu loop.
I have used Task Manager closely with my app and while in the main menu loop (at all), CPU usage hovers between 20 and 30%. This is a lot considering I'm using a quad-core PC (this is a lot regardless).
While I am in the options menu, however, I am using sGUI and calling the xSleep method method within the EventHandler object. I notice a very significant drop in CPU usage (around 5% when I move my mouse around a lot but about 1% idle).
I am curious (I guess this is a question more for Muttonhead): how does the sGUI procedure use up so little CPU, and (for anybody) why should my loop take up so much? I would ideally like to use less CPU usage than 20-30%.

I don't know why CPU usage is so different, nor how to attempt to reduce it!

The overall performance of my system while running the game is hardly affected: put the CPU figure concerns me.

LOL, this post was an accident: at least it can stay here if any find this problem and don't know the answer
Last edited by datwill310 on Apr 06, 2017 16:03, edited 1 time in total.
Boromir
Posts: 451
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: Reducing CPU Usage in Looping

Postby Boromir » Apr 06, 2017 15:59

It's because your program is hogging the cpu. You are never giving the OS a chance to do anything. On some computers this will cause your program to hang because the OS will finally steal the cpu from you program. You can solve this by putting "sleep 1" in your main loop to give the OS some time to use the cpu.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Reducing CPU Usage in Looping

Postby datwill310 » Apr 06, 2017 16:02

Boromir wrote:It's because your program is hogging the cpu. You are never giving the OS a chance to do anything. On some computers this will cause your program to hang because the OS will finally steal the cpu from you program. You can solve this by putting "sleep 1" in your main loop to give the OS some time to use the cpu.

OH wait, I thought I didn't send this... I had figured out what the problem was half way through finishing the post, and I simply closed the tab. Looks like it automatically sent it...
I'd like to remove the code: idk if it has some sensitive info - I was thinking straight...
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Reducing CPU Usage in Looping

Postby Tourist Trap » Apr 07, 2017 8:11

datwill310 wrote:I'd like to remove the code: idk if it has some sensitive info - I was thinking straight...

You can edit your post to remove anything that you think irrelevant.

About your initial question, maybe you would lilke to read this:
viewtopic.php?f=3&t=25489
viewtopic.php?f=3&t=25577

DJ.Peters shows there a nice solution to loop under the condition that events are detected (otherwise sleep). One of the two links shows you an example where I apply it (the stuff provided by DJ.Peters is embedded in the djp namespace).

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 12 guests