4

I'd like to be able to get number of nanoseconds it takes to do something in my C++ program.

Object creation, time for a function to do it's thing etc.

In Java, we'd do something along the lines of:

    long now = System.currentTimeMillis();
    // stuff
    long diff = (System.currentTimeMillis() - now);

How would you do the same in C++?

James Raitsev
  • 92,517
  • 154
  • 335
  • 470

5 Answers5

9

The <chrono> library in standard C++ provides the best way to do this. This library provides a standard, type safe, generic API for clocks.

#include <chrono>
#include <iostream>

int main() {
    using std::chrono::duration_cast;
    using std::chrono::nanoseconds;
    typedef std::chrono::high_resolution_clock clock;

    auto start = clock::now();
    // stuff
    auto end = clock::now();
    std::cout << duration_cast<nanoseconds>(end-start).count() << "ns\n";
}

The actual resolution of the clock will vary between implementations, but this code will always show results in nanoseconds, as accurately as possible given the implementation's tick period.

bames53
  • 86,085
  • 15
  • 179
  • 244
  • I am getting `error: ‘std::chrono’ has not been declared` error. What might be going on please? – James Raitsev Aug 08 '12 at 00:49
  • @Jam You may be using a standard library implementation from before C++11. What compiler version and standard library are you using? – bames53 Aug 08 '12 at 01:08
  • `i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1`, not sure about the version of standard library. How can i find it out? – James Raitsev Aug 08 '12 at 14:04
  • gcc 4.2 is far too old to support this. I'm guessing you're at least on OS X 10.6, in which case you should be able to use clang++ with the arguments -std=c++11 and -stdlib=libc++ to enable C++11 including the chrono library. – bames53 Aug 08 '12 at 15:32
  • Hmm ... I just updated the command line dev tools a few days ago. I am on OSX10.8, mountain lion. How can i upgrade the compiler please? Also, is this feature an exclusive feature of c11? – James Raitsev Aug 08 '12 at 21:17
  • Yes, chrono was introduced with C++11, but many implementations have it even before being completely C++11 compliant. The llvm-gcc compiler is not going to be updated past 4.2, and I think it has been removed starting with Xcode 4.4 (though a previous install may remain), so you have to switch to a different compiler. Use the command clang++ instead of g++ or whatever command you've been using. Also enable C++11 mode with the compiler flags -std=c++11 and -stdlib=libc++. – bames53 Aug 08 '12 at 22:06
2

In C++11 you can do it using chrono library where -

Class template std::chrono::duration represents a time interval. It consists of a count of ticks of type Rep and a tick period, where the tick period is a compile-time rational constant representing the number of seconds from one tick to the next.

Currently implemented in GCC 4.5.1. (not yet in VC++). See sample code from cppreference.com on Ideone.com execution time of a function call

SChepurin
  • 1,814
  • 25
  • 17
  • `` is in the VS2012 RC although I think the high_resolution_clock's tick is only 100ns. It should be better on other platforms. libc++ on OS X has ns resolution. I'm not sure about gcc or libstdc++. – bames53 Aug 07 '12 at 18:43
1

Take a look at clock and clock_t. For the resolution you're talking about, I don't think there's native support in C++. To get meaningful values, you'll have to time multiple calls or constructions, or use a profiler (desired).

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

Asked and answered many times.

If you're using C++11 you can consider chrono.

Community
  • 1
  • 1
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Unfortunately none of those give the right answer, ``. – bames53 Aug 07 '12 at 18:45
  • What do you mean "the right answer"? That is a C++11 feature, which the OP did not mention supporting. – Jonathon Reinhart Aug 07 '12 at 18:46
  • Chrono is standard, provides type safety for both time points vs. durations and for units, and won't require a new API as higher resolutions become available, so in most cases I'd consider it a mistake to use clock_gettime(), clock(), gettimeofday(), QueryPerformanceFrequency(), etc. – bames53 Aug 07 '12 at 18:58
  • The latest versions of all the major compilers support chrono. If someone needs to support an older compiler then they can mention that requirement. It's 2012 so IMO C++ == C++11 now. – bames53 Aug 07 '12 at 19:01
0

I asked this exact question earlier today. The best solution I have at the moment is to use SDL, and call:

uint32 a_time = SDL_GetTicks(); // Return uint32 count of milliseconds since SDL_Init was called

Although this is probably going to give you lots of overhead, even if you just init SDL with the timer functionality. (SDL_Init(SDL_INIT_TIMER).

Hope this helps you - I settled for this as a solution because it is portable.

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225