File timer.hxx

class Timer
#include <timer.hxx>

Timing class for performance benchmarking and diagnosis

To record the time spent in a particular function, create a Timer object when you wish to start timing

void someFunction() {
  Timer timer("test"); // Starts timer

} // Timer stops when goes out of scope

Each time this function is called, the total time spent in someFunction will be accumulated. To get the total time spent use getTime()

Timer::getTime("test"); // Returns time in seconds as double

To reset the timer, use resetTime

Timer::resetTime("test"); // Timer reset to zero, returning time as double

Public Types

using clock_type = typename std::conditional<std::chrono::high_resolution_clock::is_steady, std::chrono::high_resolution_clock, std::chrono::steady_clock>::type
using seconds = std::chrono::duration<double, std::chrono::seconds::period>

Public Functions


Create a timer. This constructor is equivalent to Timer(“”)

Timer(const std::string &label)

Create a timer, continuing from last time if the same label has already been used


Stop the timer

double getTime()

Get the time in seconds for time particular Timer object

Timer timer("test");
// Some calculation
output << timer.getTime();
// timer still counting

double resetTime()

Get the time in seconds, reset timer to zero

Public Static Functions

static double getTime(const std::string &label)

The total time in seconds

static double resetTime(const std::string &label)

The total time in seconds, resets the timer to zero

void cleanup()

Clears all timers, freeing memory

Private Members

timer_info &timing

The current timing information.

Private Static Functions

Timer::timer_info &getInfo(const std::string &label)

Get a timing info object by name or return a new instance.

double getTime(const timer_info &info)

Get the elapsed time in seconds for timing info.

double resetTime(timer_info &info)

Get the elapsed time, reset timing info to zero.

Private Static Attributes

std::map<std::string, Timer::timer_info> info

Store of existing timing info objects.

struct timer_info

Structure to contain timing information.

Public Members

seconds time

Total time.

bool running

Is the timer currently running?

clock_type::time_point started

Start time.

unsigned int counter

Number of Timer objects associated with this timer_info