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
Why code runs 40 times faster than other?
bilsch01


Joined: 26 Feb 2015
Posts: 55
Location: Seattle, WA, USA
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);
Why code runs 40 times faster than other?
Leonardo


Joined: 11 Feb 2010
Posts: 46
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): 
Quote:
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:
Quote:
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

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Why code runs 40 times faster than other?
Jonny D


Joined: 12 Sep 2009
Posts: 932
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 D






On Fri, Mar 13, 2015 at 1:03 PM, bilsch01 wrote:
Quote:
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

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

Why code runs 40 times faster than other?
Ken Paulson
Guest

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:
Quote:
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

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


_______________________________________________
SDL mailing list

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


Joined: 26 Feb 2015
Posts: 55
Location: Seattle, WA, USA
Thanks. I verified it is SDL_RENDERER_PRESENTVSYNC that makes it slow.
Why code runs 40 times faster than other?
Jared Maddox
Guest

Quote:
Date: Fri, 13 Mar 2015 17:03:19 +0000
From: "bilsch01"
To:
Subject: [SDL] Why code runs 40 times faster than other?
Message-ID:
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.
_______________________________________________
SDL mailing list

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