The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Using 100% CPU despite delay
dandago


Joined: 30 Aug 2013
Posts: 34
The image viewer I'm developing using SDL2 is spiking CPU to 100%. I've already done some research and tried adding a delay in my polling code (tried between 10ms and 100ms), but that didn't help at all.

I opted not to use SDL_WaitEvent() because past experience showed that it doesn't work on Mac.

So how can I keep CPU down? This is an image viewer and so user interaction is very low compared to a game.

Source code is here: https://bitbucket.org/dandago/picaxo/src
Using 100% CPU despite delay
MrOzBarry


Joined: 26 Jun 2010
Posts: 620
After line 67, do a SDL_Delay(0);

On Wed, Jan 13, 2016 at 12:33 PM, dandago wrote:
Quote:
The image viewer I'm developing using SDL2 is spiking CPU to 100%. I've already done some research and tried adding a delay in my polling code (tried between 10ms and 100ms), but that didn't help at all.

I opted not to use SDL_WaitEvent() because past experience showed that it doesn't work on Mac.

So how can I keep CPU down? This is an image viewer and so user interaction is very low compared to a game.

Source code is here: https://bitbucket.org/dandago/picaxo/src



Daniel D'Agostino
http://gigi.nullneuron.net/gigilabs/


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Using 100% CPU despite delay
Jonny D


Joined: 12 Sep 2009
Posts: 932
Alex's suggestion should do it.

Another idea for you is to move the SDL_RenderCopy() and SDL_RenderPresent() out of the "if" statement around line 166, then add the SDL_Delay() afterward.  This way if your window contents are messed with (say, by an overlapping window), it will redraw.  This will have a performance impact compared to your current method.


Also, SDL_PollEvent() returns an integer (either 0 or 1), not a pointer.  So it'd be more proper to compare to an integer than to NULL.


Jonny D








On Wed, Jan 13, 2016 at 12:44 PM, Alex Barry wrote:
Quote:
After line 67, do a SDL_Delay(0);

On Wed, Jan 13, 2016 at 12:33 PM, dandago wrote:


Quote:
The image viewer I'm developing using SDL2 is spiking CPU to 100%. I've already done some research and tried adding a delay in my polling code (tried between 10ms and 100ms), but that didn't help at all.

I opted not to use SDL_WaitEvent() because past experience showed that it doesn't work on Mac.

So how can I keep CPU down? This is an image viewer and so user interaction is very low compared to a game.

Source code is here: https://bitbucket.org/dandago/picaxo/src



Daniel D'Agostino
http://gigi.nullneuron.net/gigilabs/




_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org





_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

ollika


Joined: 28 Nov 2013
Posts: 18
Enable vsync to limit the FPS to your monitor refresh rate.

It's something like:
Code:
SDL_GL_SetSwapInterval(1);
Using 100% CPU despite delay
Daniel Gibson
Guest

On 01/13/2016 06:55 PM, ollika wrote:
Quote:
Enable vsync to limit the FPS to your monitor refresh rate.

It's something like:


Code:
SDL_GL_SetSwapInterval(1);


That only works when using OpenGL directly.
But you could pass the SDL_RENDERER_PRESENTVSYNC flag to
SDL_CreateRenderer()

Cheers,
Daniel

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Re: Using 100% CPU despite delay
ollika


Joined: 28 Nov 2013
Posts: 18
Quote:

That only works when using OpenGL directly.


Oh, I didn't even realize that. I guess the GL in the function name should have alerted me.
Using 100% CPU despite delay
Ryan C. Gordon
Guest

On 1/13/16 12:44 PM, Alex Barry wrote:
Quote:
After line 67, do a SDL_Delay(0);

(try more like SDL_Delay(10) ... I would like 0 to mean "give up the
current timeslice" but right now it might just make your OS return
immediately without giving up any CPU at all...which means in a tight
loop, you'll use 100% CPU, which is the opposite of what you wanted.)

--ryan.



_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Using 100% CPU despite delay
Ryan C. Gordon
Guest

On 1/13/16 12:55 PM, ollika wrote:
Quote:
Enable vsync to limit the FPS to your monitor refresh rate.

(but watch out, this isn't a good way to limit CPU, since your CPU might
spin waiting for vsync, or you might not be rendering frames all the
time if you are mostly reacting to input, in which case this won't help
at all.)

--ryan.



_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
dandago


Joined: 30 Aug 2013
Posts: 34
Thanks for all the comments.

My problem was that I was putting my SDL_Delay() in the inner while (the polling one), and it wasn't having any effect. Moving it out at the scope of the outer while (game loop) sorted out the problem.

Re moving the rendering functions out of the if statement - that's not necessary, it works as it is even with an overlapping window. The image is usually static, except in occasional cases where the user presses a key that causes the image to be manipulated. The if statement is there to ensure that expensive texture update operations occur only when needed.

The comment on SDL_PollEvent()'s return value is valid and I have updated the code to address this.

Thanks again, I only use SDL2 occasionally, and feedback that helps me learn is always welcome.
Using 100% CPU despite delay
MrOzBarry


Joined: 26 Jun 2010
Posts: 620
Is there a reason that this has to re-render every loop?  If you're just displaying a static non-moving image, then I'd say why bother even doing more renders?

On Wed, Jan 13, 2016 at 3:49 PM, dandago wrote:
Quote:
Thanks for all the comments.

My problem was that I was putting my SDL_Delay() in the inner while (the polling one), and it wasn't having any effect. Moving it out at the scope of the outer while (game loop) sorted out the problem.

Re moving the rendering functions out of the if statement - that's not necessary, it works as it is even with an overlapping window. The image is usually static, except in occasional cases where the user presses a key that causes the image to be manipulated. The if statement is there to ensure that expensive texture update operations occur only when needed.

The comment on SDL_PollEvent()'s return value is valid and I have updated the code to address this.

Thanks again, I only use SDL2 occasionally, and feedback that helps me learn is always welcome.



Daniel D'Agostino
http://gigi.nullneuron.net/gigilabs/


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

dandago


Joined: 30 Aug 2013
Posts: 34
Are you referring to anything specific? As I just explained, there is a check (if (updateTexture)) precisely to prevent re-rendering with iteration.
Using 100% CPU despite delay
Jonny D


Joined: 12 Sep 2009
Posts: 932
It's not just static.  It has image manipulations done to it when you press certain keys.

Jonny D






On Wed, Jan 13, 2016 at 3:51 PM, Alex Barry wrote:
Quote:
Is there a reason that this has to re-render every loop?  If you're just displaying a static non-moving image, then I'd say why bother even doing more renders?

On Wed, Jan 13, 2016 at 3:49 PM, dandago wrote:


Quote:
Thanks for all the comments.

My problem was that I was putting my SDL_Delay() in the inner while (the polling one), and it wasn't having any effect. Moving it out at the scope of the outer while (game loop) sorted out the problem.

Re moving the rendering functions out of the if statement - that's not necessary, it works as it is even with an overlapping window. The image is usually static, except in occasional cases where the user presses a key that causes the image to be manipulated. The if statement is there to ensure that expensive texture update operations occur only when needed.

The comment on SDL_PollEvent()'s return value is valid and I have updated the code to address this.

Thanks again, I only use SDL2 occasionally, and feedback that helps me learn is always welcome.



Daniel D'Agostino
http://gigi.nullneuron.net/gigilabs/




_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org





_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org