Getting precise system time

I need to get a system time as precise as possible, the time(); function returns the seconds only, could I get something more precise, maybe a function that returns ms or something even more precise?
You could always initialize with seconds and count milli-seconds manually. Qt library has a timer that could do that.

www.qt-project.org
the deal is, I'd like to have an either existing function and if there is none, create an own function. but the deal is not counting the system time, I'd like to get it...I suppose Windows has ms(at least) precision time, there should be a way to get them...
The simplest routines provided by the base Win32 API would be GetSystemTime and GetTickCount

GetSystemTime fills out a structure with the current time and data, including a milisecond field.

GetTickCount returns the number of milliseconds ellasped since the system was started.

Neither of these will provide 1 millisecond resolution. At best you'll get 10 ms.
More precise timers are not as straight forward.

Search the library on MSDN.
Last edited on
modern C++ has high_resolution_clock for this purpose:

1
2
3
4
5
6
7
8
9
#include <chrono>
#include <iostream>
int main()
{
    auto now = std::chrono::high_resolution_clock::now();
    typedef std::chrono::high_resolution_clock::period period_t;
    auto dur = now.time_since_epoch();
    std::cout << "High-res clock reports " << dur.count() << " ticks of 1/" << period_t::den << " seconds \n";
}

online demo: http://ideone.com/P4eZU
Last edited on
Thanks, that's going to do the trick!
Topic archived. No new replies allowed.