Is there a way in C++ to get nanosecond accuracy? Something equivalent to the nanosecond timer in Java (System.nanotime).
-
http://en.cppreference.com/w/cpp/chrono/steady_clock/now – chris Jul 10 '13 at 10:14
-
3You are not going to get nanosecond accuracy in a non-realtime OS. See also http://stackoverflow.com/q/5521146/96780, http://stackoverflow.com/q/1825720/96780, http://stackoverflow.com/q/1487695/96780, http://stackoverflow.com/q/275004/96780 among others. – Daniel Daranas Jul 10 '13 at 10:16
-
Nanosecond accuracy? Between what two events? – Martin James Jul 10 '13 at 11:34
-
Note that Java's `system.nanoTime()` gives nanosecond **precision** but not nanosecond **accuracy**. That is, its **units** are nanoseconds, but there are **no guarantees** about how often the value will change. – Pete Becker Jul 10 '13 at 12:06
2 Answers
If you are using Windows/Visual Studio (which might not implement std::chrono
, at least not in VS2010), you can use QueryPerformanceCounter
and QueryPerformanceFrequency
to get pretty much the highest degree of accuracy for timing tasks:
LARGE_INTEGER freq, counter1, counter2;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&counter1);
task();
QueryPerformanceCounter(&counter2);
long double elapsed = (long double) (counter2.QuadPart - counter1.QuadPart) / (long double) freq.QuadPart;

- 6,846
- 2
- 34
- 63
-
With LOTS of problems - common missync across cores (which can be several seconds and MS conveniently blames on HAL/BIOS writers), drift after CPU power saving modes kick in etc.. – Tony Delroy Jul 10 '13 at 11:37
-
I recall missync only occurs on certain dual core AMDs, and power saving isn't a huge issue if you're only using it for measuring performance over small intervals. It is still the most precise "timer" to use, though I guess the author should have specified what he wants it for. – riv Jul 10 '13 at 11:58
-
Whatever statement you recall that from may have been written when the problem was relatively new, but it's persisted and is current on modern hardware include the Intel Core i7 I'm sitting at. I wrote a library to instrument and compensate for boot-time cross-core drift about a year ago and measured significant drift on this PC at the time. I measured similar drift (multisecond sometimes) on number of modern machines in my last company - I suspect there were Xeons in there too but can't remember for sure. – Tony Delroy Jul 10 '13 at 12:07
In a C++11 implementation, you should be able to use std::chrono
.
In linux and several other Unix flavours, clock_gettime
will also give you time with nanosecond precision. In windows there is GetSystemTime
which gives millisecond precision.
However, like all of their like, it's dependent on the actual OS/Library/etc, implementation if you ACTUALLY get nanoseconds, microseconds, milliseconds or some other accuracy - the time is often presented in nanoseconds
, but that doesn't necessarily give you high accuracy - it may jump 1000000 or 10000 or 14718 ns per "tick".

- 126,704
- 14
- 140
- 227