calculating fps

Hi, i’m calculating my fps with the function in time.h: time( ). But i can only get a difference of seconds for calculating fps. It seems like there should be some way to get time in at least milliseconds. Does anyone know how?

gettimeofday() on most BSD based systems (Linux included)

timeGetTime() (or something like that) on Win32

If you are on Visual C try:



Gives time in milliseconds…


For most accurate results checkout QueryPerformanceFrequency() and QueryPerformanceCounter() which give time in processor clocks as LARGE_INTEGER (64 bit) values.

I use this for win32.

LARGE_INTEGER time, last_time, diff, frequency;

GLfloat g_frame_time;

//Initialize the variables with this.
//Get the performance counter metrics.
//Set iniitial frame time to 0.
g_frame_time = 0.f;

//Then call this each frame, to setup the current frame time in g_frame_time.

void CalcFrameTime(void)
GLfloat frame_time;

last_time = time;

diff.QuadPart = time.QuadPart - last_time.QuadPart;
frame_time = (GLfloat)diff.QuadPart / (GLfloat)frequency.QuadPart;

g_frame_time = frame_time;


Then frame rate is simple 1.0f / g_frame_time.

Use g_frame_time for movements, to make stuff frame rate independant. For example to move something at 1 unit per second, do.

position += velocity * g_frame_time;

where velocity has magnitude of 1.

Or something like that.


[This message has been edited by Nutty (edited 06-10-2001).]