Jump to content
  • Advertisement
Sign in to follow this  

elapsed time

This topic is 4164 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
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();
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;

// Before entering your game loop sample your counter so you can compute an initial interval

// some time later...

// Get the current counter value

// 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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!