Paolo M
Paolo M

Reputation: 12777

Getting time point with microseconds precision

I need to retrieve the current time point with a precision of microseconds. The time point can be relative to any fixed date.

How can it be achieved? For job policy, I really should't use boost or any other lib.

I'm working at a multiplatform application and under Linux, I can use C++11 system_clock::now().time_since_epoch(), but under Windows I work with VS2010, so I have no std::chrono library.

I've seen the RtlTimeToSecondsSince1970 function, but its resolution is a second.

Upvotes: 1

Views: 2216

Answers (3)

user3001879
user3001879

Reputation: 1

The following code works in visual studio.

#include <time.h>
clock_t start , end ;
int  getTicks_u32()
{ 
    int cpu_time_used ;
    end = clock() ;
    cpu_time_used = (static_cast<int> (end - start)) / CLOCKS_PER_SEC;
    return cpu_time_used ;
}
void initSystemClock_bl(void)
{
    start = clock();
}

Upvotes: 0

Mike Vine
Mike Vine

Reputation: 9852

Timers and timing is a tricky enough subject that In my opinion current cross platform implementations are not quite up to scratch. So I'd recommend a specific version for windows with appropriate #ifdef's. See other answers if you want a cross-platform version.

If you've got to/want to use a windows specific call then GetSystemTimeAsFileTime (or on windows 8 GetSystemTimePreciseAsFileTime) are the best calls for getting UTC time and QueryPerformanceCounter is good for high resolution timestamps. It gives back the number of 100-nanosecond intervals since January 1, 1601 UTC into a FILETIME structure.

This fine article goes into the gory details of measuring timers and timestamps in windows and is well worth a read.

EDIT: Converting a FILETIME to us, you need to go via a ULARGE_INTEGER.

FILETIME ft;
GetSystemTimeAsFileTime(&ft);
ULARGE_INTEGER li;
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
unsigned long long valueAsHns = li.QuadPart;
unsigned long long valueAsUs = valueAsHns/10;

Upvotes: 1

Serge
Serge

Reputation: 674

This code works for me in VS2010. The constructor tests to see if high-precision timing is available on the processor and currentTime() returns a time stamp in seconds. Compare time stamps for delta time. I use this for a game engine to get very small delta time values. Please note precision isn't limited to seconds despite the return value being named so (its a double).

Basically you find out how many seconds per cpu tick with QueryPerformanceFrequency and get the time using QueryPerformanceCounter.

////////////////////////
//Grabs speed of processor
////////////////////////
Timer::Timer()
{
    __int64 _iCountsPerSec = 0;
    bool _bPerfExists = QueryPerformanceFrequency((LARGE_INTEGER*)&_iCountsPerSec) != 0;
    if (_bPerfExists)
    {
        m_dSecondsPerCount = 1.0 / static_cast<double>(_iCountsPerSec);
    }
}


////////////////////////
//Returns current real time
////////////////////////
double Timer::currentTime() const
{
    __int64 time = 0;
    QueryPerformanceCounter((LARGE_INTEGER*)&time);
    double timeInSeconds = static_cast<double>(time)* m_dSecondsPerCount;
    return timeInSeconds;
}

Upvotes: 0

Related Questions