I know this probably sounds really stupid but I’m just about to try
optimizing some code but… well I was trying to figure out where to
get the frame rate so that I could use it as a basis of comparison. Is
there any quick and easy code for this?
It’s really simple. Just use SDL_GetTicks() to messure the time between
two frames.
Divide 1000 by the time (in milliseconds).On 26 Oct 2004 20:08:24 +1300, Nevyn wrote:
I know this probably sounds really stupid but I’m just about to try
optimizing some code but… well I was trying to figure out where to
get the frame rate so that I could use it as a basis of comparison. Is
there any quick and easy code for this?
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
That’s work, though it may be somewhat inaccurate-looking to you, since
the rates MAY change drastically each frame. I’m not sure how to best
capture the rate over time, but perhaps keeping a buffer of so-many
frames and when they occurred, and when you get a new one, add it to the
list and throw out the oldest? (Like a stack where the bottom one
dies?) Oh, nevermind there’s probably a better (less time-consuming)
process for doing it. Don’t liesten to me.
Hopefully someone’ll come around and correct me! ^_^;;
–Scott
Jakob Eklund wrote:> It’s really simple. Just use SDL_GetTicks() to messure the time between
two frames.
Divide 1000 by the time (in milliseconds).
Scott Harper wrote:
That’s work, though it may be somewhat inaccurate-looking to you,
since the rates MAY change drastically each frame. I’m not sure how
to best capture the rate over time, but perhaps keeping a buffer of
so-many frames and when they occurred, and when you get a new one, add
it to the list and throw out the oldest?
i use a very well working methode for counting fps, that i did not see
elsewhere:
static int
clock(void) {
struct timeval now;
gettimeofday(&now, NULL);
return
((now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000);
}
int
fps(void) {
static int frames = 0;
static int last = clock();
int f = 0, now = clock();
if (now-last>=1000) {
last = now;
f = frames;
frames = 0;
}
frames++;
return (f);
}
just call fps every frame and check if it returns != 0. i think this
is the most accurate fps counter possible, because it has no rounding
errors (it really counts the frames).
best regards …
clemens
The simplest way to get the frame rate at the end of the program is to
use (1000 * total_frames_counted / SDL_GetTicks()), but if you’re like
me and want to see the framerate in real time, you have to do a little
bit more work. The calculated frame rate for a single frame could
fluctuate wildly and print so frequently that it would be hard to read,
but you can use a rolling average to get a pretty good idea what your
frame rate has been over the last 10, 20, or 100 frames, depending on
what you need:
At program start, clear an integer array AvgFrameTimes of size NumFrames
and set an Index to 0.
At the start of each frame:
FrameTime = SDL_GetTicks();
AvgFrameTimes[Index] = FrameTime - PrevFrameTime;
Index = (Index + 1) % NumFrames;
PrevFrameTime = FrameTime;
Somewhere during each frame, when you want to print your current frame rate:
Calculate the average frame time, which is just the average of all
the numbers in the AvgFrameTimes array
The FPS value is 1000 / (average-ms-per-frame)
Just print that number to some place on the screen and you have a
real-time FPS meter.
There are some small issues with initializing your data. You’ll get
funky math problems in the first iteration if everything is set to 0 and
you don’t handle it correctly, but other than that, it’s fairly
straightforward. Not perfect (and far from optimized), but it can be
pretty useful in testing.
Ryan
Nevyn wrote:>I know this probably sounds really stupid but I’m just about to try
optimizing some code but… well I was trying to figure out where to
get the frame rate so that I could use it as a basis of comparison. Is
there any quick and easy code for this?
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
funnily enough I tried something along the times of
oldtime=newtime;
newtime=SDL_GetTicks();
printf(“FPS: %f”, 1000 / (newtime-oldtime))
This gave me a great big fat 0.00 on screen and the occassional 1.00
Had a bit more of a look and although the time was constantly changing,
(by doing a printf(“Ticks: %i”, SDL_GetTicks")) I found that newtime
and oldtime where almost always equal thus giving me a 0 result.
What does this indicate to you??On Wed, 2004-10-27 at 02:58, Scott Harper wrote:
That’s work, though it may be somewhat inaccurate-looking to you, since
the rates MAY change drastically each frame. I’m not sure how to best
capture the rate over time, but perhaps keeping a buffer of so-many
frames and when they occurred, and when you get a new one, add it to the
list and throw out the oldest? (Like a stack where the bottom one
dies?) Oh, nevermind there’s probably a better (less time-consuming)
process for doing it. Don’t liesten to me.Hopefully someone’ll come around and correct me! ^_^;;
–Scott
Jakob Eklund wrote:
It’s really simple. Just use SDL_GetTicks() to messure the time between
two frames.
Divide 1000 by the time (in milliseconds).
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
Just a thought.
Did you by any chance declare oldtime and newtime as int?
Becaus if you did you probably nead to convert them to float before the
divide:
Uint32 oldtime, newtime;
oldtime=newtime;
newtime=SDL_GetTicks();
printf(“FPS: %f”, 1000.0 / float(newtime-oldtime))
/Jakob EklundOn 28 Oct 2004 21:26:12 +1300, Nevyn wrote:
funnily enough I tried something along the times of
oldtime=newtime;
newtime=SDL_GetTicks();
printf(“FPS: %f”, 1000 / (newtime-oldtime))This gave me a great big fat 0.00 on screen and the occassional 1.00
Had a bit more of a look and although the time was constantly changing,
(by doing a printf(“Ticks: %i”, SDL_GetTicks")) I found that newtime
and oldtime where almost always equal thus giving me a 0 result.What does this indicate to you??
On Wed, 2004-10-27 at 02:58, Scott Harper wrote:
That’s work, though it may be somewhat inaccurate-looking to you, since
the rates MAY change drastically each frame. I’m not sure how to best
capture the rate over time, but perhaps keeping a buffer of so-many
frames and when they occurred, and when you get a new one, add it to the
list and throw out the oldest? (Like a stack where the bottom one
dies?) Oh, nevermind there’s probably a better (less time-consuming)
process for doing it. Don’t liesten to me.Hopefully someone’ll come around and correct me! ^_^;;
–Scott
Jakob Eklund wrote:
It’s really simple. Just use SDL_GetTicks() to messure the time
between
two frames.
Divide 1000 by the time (in milliseconds).
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
C implicit casting is “funny” sometimes…
Try 1000.0f instead of 1000, and/or a float cast of the
(newtime-oldtime) sub expression.
//David Olofson - Programmer, Composer, Open Source Advocate
.- Audiality -----------------------------------------------.
| Free/Open Source audio engine for games and multimedia. |
| MIDI, modular synthesis, real time effects, scripting,… |
`-----------------------------------> http://audiality.org -’
— http://olofson.net — http://www.reologica.se —On Thursday 28 October 2004 10.26, Nevyn wrote:
funnily enough I tried something along the times of
oldtime=newtime;
newtime=SDL_GetTicks();
printf(“FPS: %f”, 1000 / (newtime-oldtime))This gave me a great big fat 0.00 on screen and the occassional
1.00
also, are you actualy doing anything expensive in between
storing the newtime as oldtime, and then updating the newtime?
If not, oldtime and newtime will be pretty close…> ----- Original Message -----
From: sdl-bounces+kos=climaxgroup.com@libsdl.org
[mailto:sdl-bounces+kos=climaxgroup.com at libsdl.org]On Behalf Of Jakob
Eklund
Sent: 28 October 2004 11:42
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: Re: [SDL] Frame RatesJust a thought.
Did you by any chance declare oldtime and newtime as int?
Becaus if you did you probably nead to convert them to float before the
divide:Uint32 oldtime, newtime;
oldtime=newtime;
newtime=SDL_GetTicks();
printf(“FPS: %f”, 1000.0 / float(newtime-oldtime))/Jakob Eklund
On 28 Oct 2004 21:26:12 +1300, Nevyn wrote:
funnily enough I tried something along the times of
oldtime=newtime;
newtime=SDL_GetTicks();
printf(“FPS: %f”, 1000 / (newtime-oldtime))This gave me a great big fat 0.00 on screen and the occassional 1.00
Had a bit more of a look and although the time was constantly changing,
(by doing a printf(“Ticks: %i”, SDL_GetTicks")) I found that newtime
and oldtime where almost always equal thus giving me a 0 result.What does this indicate to you??
On Wed, 2004-10-27 at 02:58, Scott Harper wrote:
That’s work, though it may be somewhat inaccurate-looking to you, since
the rates MAY change drastically each frame. I’m not sure how to best
capture the rate over time, but perhaps keeping a buffer of so-many
frames and when they occurred, and when you get a new one, add it to the
list and throw out the oldest? (Like a stack where the bottom one
dies?) Oh, nevermind there’s probably a better (less time-consuming)
process for doing it. Don’t liesten to me.Hopefully someone’ll come around and correct me! ^_^;;
–Scott
Jakob Eklund wrote:
It’s really simple. Just use SDL_GetTicks() to messure the time
between
two frames.
Divide 1000 by the time (in milliseconds).
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
If oldtime and newtime are “pretty close”, you have a high frame
rate.
The delta is between the last two “loops”, and where you read the
current time and do the calculations doesn’t matter as long as the
engine does one complete loop (executing the old=new and
SDL_GetTicks() statements) per frame at all times.
//David Olofson - Programmer, Composer, Open Source Advocate
.- Audiality -----------------------------------------------.
| Free/Open Source audio engine for games and multimedia. |
| MIDI, modular synthesis, real time effects, scripting,… |
`-----------------------------------> http://audiality.org -’
— http://olofson.net — http://www.reologica.se —On Thursday 28 October 2004 12.46, Kostas Kostiadis wrote:
also, are you actualy doing anything expensive in between
storing the newtime as oldtime, and then updating the newtime?
If not, oldtime and newtime will be pretty close…
Another way might be of doing things might be measuring the time between
10 frames or so and then dividing by 10 (or so).
void framerateCheck()
{
static Uint32 oldtime, newtime,frame_count;
frame_count++;
if(frame_count %10 == 0)
{
oldtime=newtime;
newtime=SDL_GetTicks();
printf("FPS: %f", (1000.0f / float(newtime-oldtime))/10.0f )
}
}
It’s less accurate, but probably allot easier to implement.
/Jakob EklundOn Wed, 2004-10-27 at 02:58, Scott Harper wrote:
That’s work, though it may be somewhat inaccurate-looking to you, since
the rates MAY change drastically each frame. I’m not sure how to best
capture the rate over time, but perhaps keeping a buffer of so-many
frames and when they occurred, and when you get a new one, add it to the
list and throw out the oldest? (Like a stack where the bottom one
dies?) Oh, nevermind there’s probably a better (less time-consuming)
process for doing it. Don’t liesten to me.Hopefully someone’ll come around and correct me! ^_^;;
–Scott
Jakob Eklund wrote:
It’s really simple. Just use SDL_GetTicks() to messure the time between
two frames.
Divide 1000 by the time (in milliseconds).
Sorry, I should have explained this better…
You’re right, it doesn’t matter where you do the calculations
BUT if there isn’t much going on in your game loop, you may
end up with oldtime == newtime, in which case you get a div by zero.
One way would be to have something like
if (oldtime != newtime)
{
// do all the stuff
}
else
{
printf(“HUUUUUUUUUUUGE framerate\n”);
}
However, I never bother with this because there’s normally quite
a lot of stuff going on per game loop (the frame buffer flip alone
is enough to guarantee a small delay) ;-)> ----- Original Message -----
From: sdl-bounces+kos=climaxgroup.com@libsdl.org
[mailto:sdl-bounces+kos=climaxgroup.com at libsdl.org]On Behalf Of David
Olofson
Sent: 28 October 2004 11:54
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: Re: [SDL] Frame RatesOn Thursday 28 October 2004 12.46, Kostas Kostiadis wrote:
also, are you actualy doing anything expensive in between
storing the newtime as oldtime, and then updating the newtime?
If not, oldtime and newtime will be pretty close…If oldtime and newtime are “pretty close”, you have a high frame
rate.The delta is between the last two “loops”, and where you read the
current time and do the calculations doesn’t matter as long as the
engine does one complete loop (executing the old=new and
SDL_GetTicks() statements) per frame at all times.//David Olofson - Programmer, Composer, Open Source Advocate
.- Audiality -----------------------------------------------.
| Free/Open Source audio engine for games and multimedia. |
| MIDI, modular synthesis, real time effects, scripting,… |
`-----------------------------------> http://audiality.org -’
— http://olofson.net — http://www.reologica.se —
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
Ah, of course. It’s easy to forget those nasty div by zeroes, assuming
that you’ll never get that kind of figures.
//David Olofson - Programmer, Composer, Open Source Advocate
.- Audiality -----------------------------------------------.
| Free/Open Source audio engine for games and multimedia. |
| MIDI, modular synthesis, real time effects, scripting,… |
`-----------------------------------> http://audiality.org -’
— http://olofson.net — http://www.reologica.se —On Thursday 28 October 2004 13.04, Kostas Kostiadis wrote:
Sorry, I should have explained this better…
You’re right, it doesn’t matter where you do the calculations
BUT if there isn’t much going on in your game loop, you may
end up with oldtime == newtime, in which case you get a div by
zero.
lmao
I just tried that - apparently I have absolutely no problems whatsoever
with frame rate.
Here’s my output:
FPS: 1000.000000
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: 1000.000000
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: 1000.000000
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
Pretty much continues in this fashion (it’s pretty damn consistant).
Oh well… four possibilities here:
- The optimizations I had a go at really did work.
- I need to perhaps try this on a slower machine
- FPS is not an issue for 2D turn based strategy games.
- Things may become alittle more interesting when I add in some
animation…On Thu, 2004-10-28 at 23:41, Jakob Eklund wrote:
Just a thought.
Did you by any chance declare oldtime and newtime as int?
Becaus if you did you probably nead to convert them to float before the
divide:Uint32 oldtime, newtime;
oldtime=newtime;
newtime=SDL_GetTicks();
printf(“FPS: %f”, 1000.0 / float(newtime-oldtime))/Jakob Eklund
On 28 Oct 2004 21:26:12 +1300, Nevyn <@Nevyn_Hira> wrote:
funnily enough I tried something along the times of
oldtime=newtime;
newtime=SDL_GetTicks();
printf(“FPS: %f”, 1000 / (newtime-oldtime))This gave me a great big fat 0.00 on screen and the occassional 1.00
Had a bit more of a look and although the time was constantly changing,
(by doing a printf(“Ticks: %i”, SDL_GetTicks")) I found that newtime
and oldtime where almost always equal thus giving me a 0 result.What does this indicate to you??
On Wed, 2004-10-27 at 02:58, Scott Harper wrote:
That’s work, though it may be somewhat inaccurate-looking to you, since
the rates MAY change drastically each frame. I’m not sure how to best
capture the rate over time, but perhaps keeping a buffer of so-many
frames and when they occurred, and when you get a new one, add it to the
list and throw out the oldest? (Like a stack where the bottom one
dies?) Oh, nevermind there’s probably a better (less time-consuming)
process for doing it. Don’t liesten to me.Hopefully someone’ll come around and correct me! ^_^;;
–Scott
Jakob Eklund wrote:
It’s really simple. Just use SDL_GetTicks() to messure the time
between
two frames.
Divide 1000 by the time (in milliseconds).
Hmm… Try adding a SDL_Delay(5); Somewhere.On 28 Oct 2004 23:21:39 +1300, Nevyn wrote:
Pretty much continues in this fashion (it’s pretty damn consistant).
Oh well… four possibilities here:
- The optimizations I had a go at really did work.
- I need to perhaps try this on a slower machine
- FPS is not an issue for 2D turn based strategy games.
- Things may become alittle more interesting when I add in some
animation…
As I said before…if you’re not doing that much, it may
be an idea to add this
if (oldtime != newtime)
assuming oldtime and newtime are Uint32s.
If they’re not, try something like this
const float REAL_EPSILON = 1.e-6f;
if ((newtime - oldtime) > REAL_EPSILON)
{
// do your normal stuff
}
else
{
printf(“HUUUUUUUUUUUUUUUUUUUUGE framerate\n”);
}
Also, if you’re dividing, make sure either the enumerator
or the denominator are floats.
cheers,
Kos.> ----- Original Message -----
From: sdl-bounces+kos=climaxgroup.com@libsdl.org
[mailto:sdl-bounces+kos=climaxgroup.com at libsdl.org]On Behalf Of Nevyn
Sent: 28 October 2004 11:22
To: A list for developers using the “SDL library. (includes
”"SDL-announce)
Subject: Re: [SDL] Frame Rateslmao
I just tried that - apparently I have absolutely no problems whatsoever
with frame rate.Here’s my output:
FPS: 1000.000000
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: 1000.000000
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: 1000.000000
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: inf
FPS: infPretty much continues in this fashion (it’s pretty damn consistant).
Oh well… four possibilities here:
- The optimizations I had a go at really did work.
- I need to perhaps try this on a slower machine
- FPS is not an issue for 2D turn based strategy games.
- Things may become alittle more interesting when I add in some
animation…On Thu, 2004-10-28 at 23:41, Jakob Eklund wrote:
Just a thought.
Did you by any chance declare oldtime and newtime as int?
Becaus if you did you probably nead to convert them to float before the
divide:
Uint32 oldtime, newtime;
oldtime=newtime;
newtime=SDL_GetTicks();
printf(“FPS: %f”, 1000.0 / float(newtime-oldtime))/Jakob Eklund
On 28 Oct 2004 21:26:12 +1300, Nevyn wrote:
funnily enough I tried something along the times of
oldtime=newtime;
newtime=SDL_GetTicks();
printf(“FPS: %f”, 1000 / (newtime-oldtime))This gave me a great big fat 0.00 on screen and the occassional 1.00
Had a bit more of a look and although the time was constantly changing,
(by doing a printf(“Ticks: %i”, SDL_GetTicks")) I found that newtime
and oldtime where almost always equal thus giving me a 0 result.What does this indicate to you??
On Wed, 2004-10-27 at 02:58, Scott Harper wrote:
That’s work, though it may be somewhat inaccurate-looking to you, since
the rates MAY change drastically each frame. I’m not sure how to best
capture the rate over time, but perhaps keeping a buffer of so-many
frames and when they occurred, and when you get a new one, add it to
thelist and throw out the oldest? (Like a stack where the bottom one
dies?) Oh, nevermind there’s probably a better (less time-consuming)
process for doing it. Don’t liesten to me.Hopefully someone’ll come around and correct me! ^_^;;
–Scott
Jakob Eklund wrote:
It’s really simple. Just use SDL_GetTicks() to messure the time
between
two frames.
Divide 1000 by the time (in milliseconds).
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
Jakob Eklund <mailinglists koalas.dk> writes:> On 26 Oct 2004 20:08:24 +1300, Nevyn <nevyn woosh.co.nz> wrote:
I know this probably sounds really stupid but I’m just about to try
optimizing some code but… well I was trying to figure out where to
get the frame rate so that I could use it as a basis of comparison. Is
there any quick and easy code for this?It’s really simple. Just use SDL_GetTicks() to messure the time between
two frames.
Divide 1000 by the time (in milliseconds).
That’ll work, though it’ll bounce around a lot as the time between frames varies
slightly. You can also have one variable storing a time, and another variable
counting frames. Start frames at 0, and store the time. Each frame, put the
frame counter up, and check if the current time is the stored time + 1000. When
it is, print out the counter as the current FPS, then reset it to 0 and put the
current time in the stored time. Then the FPS will update every second, showing
the average FPS over the last second.
Nick wrote:
Jakob Eklund <mailinglists koalas.dk> writes:
I know this probably sounds really stupid but I’m just about to
try optimizing some code but… well I was trying to figure out
where to get the frame rate so that I could use it as a basis of
comparison. Is there any quick and easy code for this?It’s really simple. Just use SDL_GetTicks() to messure the time
between two frames.
Divide 1000 by the time (in milliseconds).That’ll work, though it’ll bounce around a lot as the time between
frames varies slightly. You can also have one variable storing a time,
and another variable counting frames. Start frames at 0, and store the
time. Each frame, put the frame counter up, and check if the current
time is the stored time + 1000. When it is, print out the counter as
the current FPS, then reset it to 0 and put the current time in the
stored time. Then the FPS will update every second, showing the
average FPS over the last second.
i posted code for this a few days ago, either it passed away unnoticed
or the mail didn’t show up in the list. ithink this is the best
methode because it fixes two problems: it is very accurate, becaus there
are no rounding errors and it does not produce to_many numbers.
regards …
clemens> > On 26 Oct 2004 20:08:24 +1300, Nevyn <nevyn woosh.co.nz> wrote: