9

Possible Duplicate:
measuring time with resolution of microseconds in c++?

Hi,

Is there a simple way i can get the system time on a Windows machine, down to microsecond accuracy?

Community
  • 1
  • 1
Daniel
  • 91
  • 1
  • 1
  • 3

3 Answers3

12

Look at GetSystemTimeAsFileTime

It gives you accuracy in 0.1 microseconds or 100 nanoseconds.

Note that it's Epoch different from POSIX Epoch.

So to get POSIX time in microseconds you need:

    FILETIME ft;
    GetSystemTimeAsFileTime(&ft);
    unsigned long long tt = ft.dwHighDateTime;
    tt <<=32;
    tt |= ft.dwLowDateTime;
    tt /=10;
    tt -= 11644473600000000ULL;

So in such case time(0) == tt / 1000000

Artyom
  • 31,019
  • 21
  • 127
  • 215
  • 3
    More likely it's the time expressed in 100-nanoseconds rather than 100-nanosecond accuracy? – Steve-o Dec 31 '10 at 07:52
  • 1
    You could also use `GetSystemTime()`... but the issue is, it's absolute time, not relative time. It isn't good for use inside a program to figure out time elapsed, as absolute time can change for various random reasons (e.g. time sync). – user541686 Dec 31 '10 at 08:36
  • @Lambert GetSystemTime as milliseconds accuracy, it is not good enough, also it used date format so it is hard to calculate differences – Artyom Dec 31 '10 at 09:42
  • @Steve-o yes, you are right. But it is the most accurate measurement that Win32API can give – Artyom Dec 31 '10 at 09:42
  • 6
    From `KeQuerySystemTime`: "System time is typically updated approximately every ten milliseconds." – wj32 Dec 31 '10 at 09:56
  • " GetSystemTime as milliseconds accuracy, it is not good enough" -- So converting it to a file time will magically make it more accurate? "the most accurate measurement that Win32API can give" -- directly, but not the most accurate that can be calculated. The interval is 10 ms, but the precision of that 10 ms is very high, as can be determined by using QueryPerformanceCounter. Spin on that until GetSystemTime changes, and you can do very accurate timing. – Jim Balter Aug 28 '12 at 00:35
  • I'm not sure how to interpret the output of "tt/1000000". For example, I'm getting 1350054353. Could someone help me out? – John Roberts Oct 12 '12 at 15:06
  • You are confusing *accuracy* with *precision*. – IInspectable Dec 24 '17 at 22:37
  • See also: `GetSystemTimePreciseAsFileTime()` – KJ7LNW Aug 28 '23 at 19:53
4

Like this

unsigned __int64 freq;
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
double timerFrequency = (1.0/freq);

unsigned __int64 startTime;
QueryPerformanceCounter((LARGE_INTEGER *)&startTime);

//do something...

unsigned __int64 endTime;
QueryPerformanceCounter((LARGE_INTEGER *)&endTime);
double timeDifferenceInMilliseconds = ((endTime-startTime) * timerFrequency);
Darcara
  • 1,598
  • 1
  • 13
  • 33
  • 6
    QueryPerformanceCounter does not returns **system time** but high resolution **counter**. – 9dan Dec 31 '10 at 07:33
-1

What we really need is a high-resolution GetTickCount(). As far as I know, this doesn't really exist.

If you're willing to use a hackish way to solve this (that would probably only work on some versions of Windows like XP), look here at ReactOS. Then try this code:

long long GetTickCount64()
{
    return (long long)
        ((((unsigned long long)*(unsigned long int*)0x7FFE0000
           * (unsigned long long)*(unsigned long int*)0x7FFE0004)
         * (unsigned long long)10000) >> 0x18);
}

Tweaking it might give you what you need in some versions of Windows.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • GetTickCount is always in milliseconds. The 32-bit GetTickCount implementation you posted is no different from the old Win32 implementation. – wj32 Dec 31 '10 at 09:50
  • Exactly -- that's why I said **tweak** it. You can do smaller bit shifts or multiply by a smaller number, and get more accuracy. – user541686 Dec 31 '10 at 09:51
  • My bad. Check out KeQueryTimeIncrement to convert to a time value. – wj32 Dec 31 '10 at 09:56
  • @wj32: Except that you can only call that from kernel mode... – user541686 Dec 31 '10 at 09:56
  • 1
    True, but I did say "Check out" the function. Well, it turns out you have the system call `NtQueryTimerResolution` which returns this exact value (the time increment). – wj32 Dec 31 '10 at 09:58
  • @wj32: Nice job finding that! It's undocumented but much less hackish than my solution; I like it! +1 for the comment. :) – user541686 Dec 31 '10 at 09:59
  • Note: it works for `KeQueryTickCount`, but I don't know what the relationship is between `KeQueryTickCount` and the USER_SHARED_DATA structure which your `NtGetTickCount64` function uses. CBB doing more disassembling. – wj32 Dec 31 '10 at 10:02
  • Another hackish way would be to use `NtQuerySystemInformation` with `SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION` and adding up the values... – user541686 Dec 31 '10 at 10:07