Why code runs 40 times faster than other?

I have two versions of a program that plots a graph. One is about 40 times faster. The only difference between them is a group of SDL lines. The groups are shown below. Can anyone tell me why the difference in speed? TIA. Bill S.

/* THESE LINES ARE FAST /
SDL_Init(SDL_INIT_VIDEO);
SDL_Window
win1 = NULL;
win1 = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* ren = NULL;
ren = SDL_CreateRenderer( win1, 0, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor( ren, 0, 0, 0, 255 );
SDL_RenderClear( ren );

/* THESE LINES ARE SLOW */
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *win1 = NULL;
win1 = SDL_CreateWindow("", 0, 0, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer *ren = SDL_CreateRenderer( win1, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_SetRenderDrawColor( ren, 0, 0, 0, 255 );
SDL_RenderClear(ren);

My guess the reason is the flag SDL_RENDERER_PRESENTVSYNC that you’re
creating the render with.

Per SDL docs/wiki on SDL_CreateRenderer (
https://wiki.libsdl.org/SDL_CreateRenderer):

SDL_RENDERER_PRESENTVSYNC: present is synchronized with the refresh rate

Meaning SDL_RenderPresent waits until the correct time to do its job and
then return, effectively controlling your frame rate.

If you want your iterations to be as fast as possible, maybe you shouldn’t
use this flag or you should put your simulation code in another thread (but
thats a whole different can of problems, imho).

HTH

Leonardo.Em sex, 13 de mar de 2015 ?s 14:03, bilsch01 escreveu:

I have two versions of a program that plots a graph. One is about 40
times faster. The only difference between them is a group of SDL lines. The
groups are shown below. Can anyone tell me why the difference in speed?
TIA. Bill S.

/* THESE LINES ARE FAST /
SDL_Init(SDL_INIT_VIDEO);
SDL_Window
win1 = NULL;
win1 = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* ren = NULL;
ren = SDL_CreateRenderer( win1, 0, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor( ren, 0, 0, 0, 255 );
SDL_RenderClear( ren );

/* THESE LINES ARE SLOW */
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *win1 = NULL;
win1 = SDL_CreateWindow("", 0, 0, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer *ren = SDL_CreateRenderer( win1, -1, SDL_RENDERER_ACCELERATED
| SDL_RENDERER_PRESENTVSYNC);
SDL_SetRenderDrawColor( ren, 0, 0, 0, 255 );
SDL_RenderClear(ren);


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I would suppose it’s vsync limiting you to (typically) 60 fps on the "slow"
code. Note: vsync is a good thing for most programs.

Jonny DOn Fri, Mar 13, 2015 at 1:03 PM, bilsch01 wrote:

I have two versions of a program that plots a graph. One is about 40
times faster. The only difference between them is a group of SDL lines. The
groups are shown below. Can anyone tell me why the difference in speed?
TIA. Bill S.

/* THESE LINES ARE FAST /
SDL_Init(SDL_INIT_VIDEO);
SDL_Window
win1 = NULL;
win1 = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* ren = NULL;
ren = SDL_CreateRenderer( win1, 0, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor( ren, 0, 0, 0, 255 );
SDL_RenderClear( ren );

/* THESE LINES ARE SLOW */
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *win1 = NULL;
win1 = SDL_CreateWindow("", 0, 0, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer *ren = SDL_CreateRenderer( win1, -1, SDL_RENDERER_ACCELERATED
| SDL_RENDERER_PRESENTVSYNC);
SDL_SetRenderDrawColor( ren, 0, 0, 0, 255 );
SDL_RenderClear(ren);


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

If you look at the documentation you will see this:
SDL_RENDERER_PRESENTVSYNC: present is synchronized with the refresh rate

So if the refresh rate of your monitor is 60Hz, then your code will only
be able to run at 60 fps.On 13/03/2015 1:03 PM, bilsch01 wrote:

I have two versions of a program that plots a graph. One is about 40
times faster. The only difference between them is a group of SDL lines.
The groups are shown below. Can anyone tell me why the difference in
speed? TIA. Bill S.

/* THESE LINES ARE FAST /
SDL_Init(SDL_INIT_VIDEO);
SDL_Window
win1 = NULL;
win1 = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* ren = NULL;
ren = SDL_CreateRenderer( win1, 0, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor( ren, 0, 0, 0, 255 );
SDL_RenderClear( ren );

/* THESE LINES ARE SLOW */
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *win1 = NULL;
win1 = SDL_CreateWindow("", 0, 0, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer *ren = SDL_CreateRenderer( win1, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_SetRenderDrawColor( ren, 0, 0, 0, 255 );
SDL_RenderClear(ren);


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks. I verified it is SDL_RENDERER_PRESENTVSYNC that makes it slow.

Message-ID: <1426266199.m2f.47273 at forums.libsdl.org>
Content-Type: text/plain; charset=“iso-8859-1”

I have two versions of a program that plots a graph. One is about 40 times
faster. The only difference between them is a group of SDL lines. The
groups are shown below. Can anyone tell me why the difference in speed?
TIA. Bill S.

As others have said, it’s the VSync. You say that you’re rendering a
graph, I assume of a mathematical nature (as opposed to a render
graph). Are you saving the rendered graph for external use, or is it
purely a “real-time” display?

  1. If you’re saving the rendered graphs, then I’d suggest Leonardo’s
    suggestion, + render targets.
  2. Otherwise, I’d suggest assessing your code to see if the behavior
    of the similation is affected by render time:
    2 I ) If it is, then I’d suggest Leonado’s suggestion again, but
    without render targets this time,
    2 II ) If it isn’t affected, then you should usually (or even always)
    be fine with VSync.

How did you notice the speed difference? Speed differences produced by
VSync are normally only a VALID concern if the speed either impedes
development, or during the shift from “developing” status to
"publishing" status. The vast majority of those who post about the
subject seem to be trying to optimize too early into their development
efforts.> Date: Fri, 13 Mar 2015 17:03:19 +0000

From: “bilsch01”
To: sdl at lists.libsdl.org
Subject: [SDL] Why code runs 40 times faster than other?