Code snippets‎ > ‎C++‎ > ‎


Intel TBB

The file below is supposed to use the most accurate timer available on the platform. It is adapted from Intel TBB to be used as a stand-alone header file.

Standard library - std

clock_t clock ( void );

Clock program Returns the number of clock ticks elapsed since the program was launched.

The macro constant expression CLOCKS_PER_SEC specifies the relation between a clock tick and a second (clock ticks per second).

The initial moment of reference used by clock as the beginning of the program execution may vary between platforms. To calculate the actual processing times of a program, the value returned by clock should be compared to a value returned by an initial call to clock.

#include <ctime> 

int main() {
  std::clock_t start = std::clock();
  std::clock_t stop = std::clock(); 
  double diff = (stop-start)/(double)CLOCKS_PER_SEC;
  std::cout << "Elapsed time " << diff << "s" << std::endl;

boost - C++ libraries

#include <boost/timer.hpp>

boost::timer t;
double diff = t.elapsed();

Nico Kruithof,
Aug 23, 2013, 7:12 AM