Possible Duplicate:
C++ Timer function to provide time in nano seconds
I need to measure the duration of a function execution in nanoseconds resolution. Is it possible? Our ordinary computer hardware and software are able to give such a precision for time? If yes how to accomplish that with c++? Is it possible with Boost library?
Yes, today most hardware supports this sort of resolution, and the C++ standard library has an API that can support it as well. Unfortunately not all implementations of C++ actually do provide it.
The API is the
<chrono>library introduced in C++11:The
<chrono>implementation in libc++ for Darwin provides nanosecond resolution, but it seems the implementation in VS2012 only goes to tenths of milliseconds. The above code will still give times in terms of nanoseconds, but timings less than 100 microseconds will end up being zero nanoseconds.Boost also provides an implemenation, boost::chrono, which does seem to use nanoseconds on Windows. It’s also usable with C++03.