Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to do it using two FILETIMEs, casting them to ULONGLONGs, substracting the ULONGLONGs, and dividing the result by 10000. But it's pretty slow, and I want to know if there is a better way to do it.I use c++ with visual studio 2008 express edition. This is what I'm using:

FILETIME filetime,filetime2;
GetSystemTimeAsFileTime(&filetime);
Sleep(100);
GetSystemTimeAsFileTime(&filetime2);
ULONGLONG time1,time2;
time1 = (((ULONGLONG) filetime.dwHighDateTime) << 32) + filetime.dwLowDateTime;
time2 = (((ULONGLONG) filetime2.dwHighDateTime) << 32) + filetime2.dwLowDateTime;
printf("ELAPSED TIME IN MS:%d",(int)((time2-time1)/10000));
share|improve this question

3 Answers

up vote 3 down vote accepted

Use QueryPerformanceCounter.

long long milliseconds_now() {
    static LARGE_INTEGER s_frequency;
    static BOOL s_use_qpc = QueryPerformanceFrequency(&s_frequency);
    if (s_use_qpc) {
        LARGE_INTEGER now;
        QueryPerformanceCounter(&now);
        return (1000LL * now.QuadPart) / s_frequency.QuadPart;
    } else {
        return GetTickCount();
    }
}

// Somewhere else...
    // ...
    long long start = milliseconds_now();
    // ....
    long long elapsed = milliseconds_now() - start;
share|improve this answer
And would that run fast? – XaitormanX Apr 4 '12 at 13:15
Fast enough to call a dozen times during a frame without noticeable impact. How many times do you expect to need it? – Joe Wreschnig Apr 4 '12 at 13:21
about 4 times a frame. But if i put that code in a loop to do it 100 times, it takes about 10 seconds to execute, which is slow, why is this happening? – XaitormanX Apr 4 '12 at 13:24
1  
You have a Sleep(100) in your code. It has nothing to do with the timers. – Joe Wreschnig Apr 4 '12 at 13:28
1  
It ran fast on a p90 in 1996; I think it'll run fast enough today. ;) – mh01 Apr 4 '12 at 18:17
show 1 more comment

To answer your question : there is no "best" solution to measure time. Every method has its own advantages/disadvantages. It all depends of your needs.

Here is several questions you should ask yourself before choosing one solution :

  • timer resolution. Do you really need a precise timer (< 1ms) or can you live with something less precise (eg : 10ms precision) ?

  • cpu cost. some methods will require more cpu than some others. generally more precise timers require more cpu. also, calling some methods can have consequences for the other applications (eg : calling timeBeginPeriod(1) in win32 stress the os and reduce laptop battery life)

  • works with SMP. some methods use specific CPU data (eg: number of cycles elapsed) and can have some problems in multiprocessor environments.

  • works with CPU that have power saving feature. Some cpu have ability to change frequency over the time, which can cause some trouble to some timing methods.

For the example you give, I would use a combination of QueryPerformanceCounter() / QueryPerformanceFrequency().

Also please note that in the example you give sleep(100) can actually wait more than 100ms. So even with very precise timer it can returns some results other than 100ms.

share|improve this answer
While it's true that some timing methods have problems on SMP, these are always bugs in the HAL, BIOS, or other driver-related problems, and must be fixed at that level. It's rare that you can solve the problem by switching to a different timing method. For example I've seen QPC run backwards because of bad drivers, but GetTickCount would have too - the only reason it didn't was because the jump was ~50µsec, so it didn't notice it at all. At best, your app can do a max call with the previously returned value. – Joe Wreschnig Apr 4 '12 at 14:37
As it is, QPC and equivalent functions on other OSs like POSIX clock_gettime, are all specified to not run backwards even on SMP systems, and independent of current CPU frequency. So it's bad advice to caution people away from them and towards worse timing functions, because any other timing functions would likely suffer the same problems if you could ever get them to work as precisely as you wanted from QPC in the first place. So yeah, there is a best solution: QPC, clock_gettime, and mach_absolute_time. For game loop timing, there's no reason to use anything else if those are available. – Joe Wreschnig Apr 4 '12 at 14:40

Take a look at the GetTickCount() function it's return value is the number of milliseconds that have elapsed since the system was started.

unsigned int Last = 0;
unsigned int Now = 0;
Last = GetTickCount();
//...Run your functions..
Now = GetTickCount();
printf("Elapsed Time in Milliseconds: %i",(Now-Last));
share|improve this answer
6  
-1. GetTickCount() has poor resolution. – Joe Wreschnig Apr 4 '12 at 13:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.