Public Group

# elapsed time

This topic is 3986 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

whats a fast way of getting elapsed time in miliseconds? i was using int y,elapsed; y=clock() //do stuff elapsed=clock()-y; but then i realised clock is processor time.

##### Share on other sites
DWORD timeGetTime(VOID); This will return the number of milliseconds since the computer turned on. (Will overflow after 49 days or so!) But you could use your exact idea:

DWORD startTime = timeGetTime();
//CODE HERE
DWORD endTime = timeGetTime();
DWORD elapsedTime = endTime - startTime;

I believe you need to include time.h but I am not positive what actually include to use.

##### Share on other sites
If you're developing for Windows, the most accurate and elegant solution by far is a pair of win32 functions called "QueryPerformanceFrequency" and "QueryPerformanceCounter".

QueryPerformanceFrequency tells you how many clock cycles your computer will go through in 1 second.

QueryPerformanceCounter tells you the count of the current clock cycles since your computer was last turned on.

By sampling the QueryPerformanceCounter at two points in time and subtracting, you can tell the exact number of CPU cycles have elapsed.

Then, by divining the number of Cycles by the frequency obtained from QueryPerformanceFrequency you can determine the amount of time elapsed with up to 64 bits of precision.

The code looks like this:

// Calculate your CPU frequency once during your applicationLARGE_INTEGER frequency;QueryPerformanceFrequency(&frequency);// Before entering your game loop sample your counter so you can compute an initial intervalLARGE_INTEGER previous;QueryPerformanceCounter(&previous);// some time later...// Get the current counter valueLARGE_INTEGER current;QueryPerformanceCounter(&current);// Compute the interval to determine how much time has elapsedLARGE_INTERGER interval = current.QuadPart - previous.QuadPart;double elapsedTime = interval.QuadPart / frequency.QuadPart;// Store the current time as the new previous time, wash, rinse, repeat...previous = current;

• 9
• 23
• 10
• 19