Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C get system time to microsecond accuracy on windows? [duplicate]

Tags:

c++

c

windows

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?

like image 488
Daniel Avatar asked Dec 31 '10 05:12

Daniel


2 Answers

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

like image 192
Artyom Avatar answered Nov 18 '22 02:11

Artyom


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);
like image 4
Darcara Avatar answered Nov 18 '22 00:11

Darcara