Jump to content
  • Advertisement
Sign in to follow this  
Ksingh30

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 application
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);

// Before entering your game loop sample your counter so you can compute an initial interval
LARGE_INTEGER previous;
QueryPerformanceCounter(&previous);

// some time later...

// Get the current counter value
LARGE_INTEGER current;
QueryPerformanceCounter(&current);

// Compute the interval to determine how much time has elapsed
LARGE_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;


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!