30

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?

Community
  • 1
  • 1
Narek
  • 38,779
  • 79
  • 233
  • 389
  • what computer do you have? what the speed of the CPU? – elyashiv Oct 17 '12 at 15:37
  • 1
    Already asked: http://stackoverflow.com/questions/275004/c-timer-function-to-provide-time-in-nano-seconds – dario_ramos Oct 17 '12 at 15:38
  • @Narek from that you can calculate whether the precision you want can be achieved or not. –  Oct 17 '12 at 15:40
  • You can use RDTSC if the intrinsic is available on your compiler, and your hardware supports it (i5 should). You will find a lot of people that direct you _away_ from RDTSC, as using it appropriately can be a challenge, but for very small measurements of time you can't get more accurate time slice representations. See the 2nd answer in the duplicate question for an example. – Chad Oct 17 '12 at 16:01
  • Everyone came and considered as duplicate, but now I have no answer to my question. I don't know how to benchmark a function execution duration in NANOSECONDS on Windows with old C++! – Narek Oct 18 '12 at 13:19

4 Answers4

64

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:

#include <iostream>
#include <chrono>

int main() {
    auto start = std::chrono::high_resolution_clock::now();

    // operation to be timed ...

    auto finish = std::chrono::high_resolution_clock::now();
    std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(finish-start).count() << "ns\n";
}

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.

#include <boost/chrono.hpp>

int main() {
    boost::chrono::high_resolution_clock::time_point t1 =
        boost::chrono::high_resolution_clock::now();

    boost::chrono::high_resolution_clock::time_point t2 =
        boost::chrono::high_resolution_clock::now();

    std::cout << boost::chrono::duration_cast<boost::chrono::nanoseconds>(t2-t1) << "\n";
    // boost chrono has more advanced output, .count() isn't needed, nor is manually writing out the units
}
bames53
  • 86,085
  • 15
  • 179
  • 244
3

I'm not sure if you're looking for clock or difftime, but one of these should be what you're looking for:

http://www.cplusplus.com/reference/clibrary/ctime/clock/

http://www.cplusplus.com/reference/clibrary/ctime/difftime/

You can use this before and after functions and compare the differences of runtimes to see the overall runtime of a function.

mttalxndrgrrtt
  • 111
  • 1
  • 6
3

About the best you can do with the Windows API is QueryPerformanceCounter. You can obtain its resolution with QueryPerformanceFrequency. Depending on the situation, this may be use the CPU's RDTSC instruction, in which case the frequency is basically the clock frequency of the CPU. In other cases, it uses a separate clock with a frequency of 1.024 MHz, so the resolution is basically one microsecond.

C++11 adds a chrono class that may be useful as well -- assuming you're using a compiler that already has it. If your compiler doesn't provide an implementation, Boost Chrono is a reasonable substitute that works with most existing compilers/libraries. Again, there's no guarantee that it'll provide nanosecond resolution, but I'd expect that on Windows it'll probably be a portable wrapper around QPC/QPF, so it'll probably give the same resolution they do, just more portably.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

And of course there is also the good old Boost::date_time :

#include <boost/date_time/posix_time/posix_time.hpp>

boost::posix_time::ptime date_time = boost::posix_time::microsec_clock::universal_time();

microsec only, though.

Offirmo
  • 18,962
  • 12
  • 76
  • 97