1

I just started programming C++ in Linux, can anyone recommend a good way for measurement of code elapsed time, ideally to nanoseconds precision, but milli-seconds will do as well.

And also a fast printing method, I am using std::cout at the moment, but I feel it's kind of slow.

Thanks.

2607
  • 4,037
  • 13
  • 49
  • 64

6 Answers6

3

You could use gettimeofday, or clock_gettime.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

For timing you can use the <chrono> standard library:

#include <chrono>
#include <iostream>

int main() {
    using Clock = std::chrono::high_resolution_clock;
    using std::chrono::milliseconds;
    using std::chrono::nanoseconds;
    using std::chrono::duration_cast;

    auto start = Clock::now();

    // code to time
    std::this_thread::sleep_for(milliseconds(500));

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

The actual clock resolution depends on the implementation, but this will always output the correct units.

The performance of std::cout depends on the implementation as well. IME, as long as you don't use std::endl everywhere its performance compares quite well with printf on Linux or OS X. Microsoft's implementation in VC++ seems to be much slower.

bames53
  • 86,085
  • 15
  • 179
  • 244
1

To get a time in nanoseconds, use clock_gettime(). To measure an elapsed time taken by the code, CLOCK_MONOTONIC_RAW clock type must be used. Using other clock types is not really a solution because they are subject to NTP adjustments.

As for the printing part - define slow. A "general" code to convert built-in data types into ASCII strings is always slow. There is also a buffering going on (which is good in most cases). If you can make some good assumptions about your data, you can always throw in your own conversion to ASCII which will beat a general-purpose solutions, and make it faster.

EDIT:

See also an example of using clock_gettime() function and OS X specific mach_absolute_time() functions here:

0

Printing things is normally slow because of the terminal you're watching it in, rather than because you're printing something in the first place. You can redirect output to a file, then you might see a significant speedup if you're printing a lot to the console.

James
  • 24,676
  • 13
  • 84
  • 130
  • Although I generally agree with your comment about the console being slow, typical implementations of IOstreams are adding their bit to slowness as well. It is worth noting, however, that this isn't down to the specification. Unfortunately, it isn't entirely simple to create a really fast implementation of the IOstreams library although I'm still convinced that it can be faster than stdio. – Dietmar Kühl Feb 26 '12 at 22:07
  • 2
    @DietmarKühl True, and it's a bit inexcusable since they have the types at compile-time which ought to allow all sorts of wonderful optimisation. I think most people's perception of the relative speed is probably affected by the fact `endl` flushes, too. – James Feb 26 '12 at 22:24
  • well, knowing the type at compile time is pretty much offset by the added flexibility of allowing users to provide functions to format everything, send characters to arbitrary destination, convert characters, etc. However, with a bit of caching and being smart about when to do work, this can be worked around. – Dietmar Kühl Feb 26 '12 at 23:42
0

I think you probably also want to have a look at the time [0] command, which reports the time taken by a specific program to complete execution.

[0] http://linux.about.com/library/cmd/blcmdl1_time.htm

reddragon
  • 961
  • 8
  • 17
0

Time measurement:

Boost.Chrono: http://www.boost.org/doc/libs/release/doc/html/chrono.html // note that if you have a modern C++11 (used to be C++0x) compiler you already have this out of the box, since "Boost.Chrono aims to implement the new time facilities in C++0x, as proposed in N2661 - A Foundation to Sleep On."

Boost.Timer: http://www.boost.org/doc/libs/release/libs/timer/

Posix Time from Boost.Date_Time: http://www.boost.org/doc/libs/release/doc/html/date_time/posix_time.html

Fast printing:

FastFormat: http://www.fastformat.org/

Benchmarks: http://www.fastformat.org/performance.html

Regarding the performance of C++ streams -- remember about std::ios_base::sync_with_stdio, see:

http://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio

http://www.cplusplus.com/reference/iostream/ios_base/sync_with_stdio/

Matt
  • 629
  • 9
  • 21