I am using an opengl es iphone application. What is the most accurate way to calculate the frames per second of my application for performance tuning?
Find something like a high resolution timer (more than 1000 ticks/seconds), and measure the time between when you start rendering until it's on the screen.
Divide the ticks per second by the time you just measured, and you have the FPS.
Check this thread out. There are some very useful links on it. Good luck!
Try running:
Run -> Run With Performance Tool -> OpenGL ES
.
You have to run that tool when connected to the device (which you'd obviously want to anyway for performance tuning...)
It gives you Core Animation FPS which may (probably) not be what you are looking for, but it can graph a number of other useful statistics for you which may also help you optimize.
Measuring FPS during development is nearly meaningless. Measure the rendering time you need!
This way you will have a much clearer understanding of how your changes affect performance. Usually it's a very good idea to split your rendering time into meaningful slices, e.g. "render background", "render level", "render mobs", etc.
If you still want to do FPS my personal favourite is Johns NSTimer version, even if it doesn't show spikes.
I guess what you want is this:
// fps calculation
static int m_nFps; // current FPS
static CFTimeInterval lastFrameStartTime;
static int m_nAverageFps; // the average FPS over 15 frames
static int m_nAverageFpsCounter;
static int m_nAverageFpsSum;
static UInt8* m_pRGBimage;
static void calcFps()
{
CFTimeInterval thisFrameStartTime = CFAbsoluteTimeGetCurrent();
float deltaTimeInSeconds = thisFrameStartTime - lastFrameStartTime;
m_nFps = (deltaTimeInSeconds == 0) ? 0: 1 / (deltaTimeInSeconds);
m_nAverageFpsCounter++;
m_nAverageFpsSum+=m_nFps;
if (m_nAverageFpsCounter >= 15) // calculate average FPS over 15 frames
{
m_nAverageFps = m_nAverageFpsSum/m_nAverageFpsCounter;
m_nAverageFpsCounter = 0;
m_nAverageFpsSum = 0;
}
lastFrameStartTime = thisFrameStartTime;
}
Regards, Asaf Pinhassi.