PrecTimer.h

Classes

PrecTimer -- Precision timer to measure elapsed times in a cumulative way (full description)

class PrecTimer

Interface

Public Members
PrecTimer()
~PrecTimer()
void start()
void stop()
void reset()
void show() const
void show (std::ostream& os) const
void show (const String&) const
void show (std::ostream& os, const ostream& prefix) const
double getReal() const
unsigned long long getCount() const
Private Members
void print_time (std::ostream&, double time) const
static double get_CPU_speed_in_MHz()

Description

Review Status

Programs:
Tests:

Synopsis

The PrecTimer supplements the Timer class. If offers a low-overhead and high-resolution interval timer for use on i386, x86_64, ia64, and powerpc platforms, using the processor's timestamp counter that is incremented each cycle. Put timer.start() and timer.stop() calls around the piece of code to be timed. Because the timer is cumulative, the total time of a particular piece of code can be timed.
Caution Make sure that start() and stop() calls alternate, otherwise very strange times will be the result.

A timer can be started and stopped multiple times; both the average and total time, as well as the number of iterations are printed. The measured time is real time (as opposed to user or system time). The timer can be used to measure from 10 nanosecond to a century interval.

Multiple timers can be used in a nested way as long as each of them has independent (matching) start and stop calls.

The class is more or less a copy of the original written by John Romein at ASTRON, Dwingeloo, the Netherlands.

Example

Here's how to create a timer, start it (the 'mark' member function) and display a breakdown.
  PrecTimer ttimer;   // the timer is reset at construction time
  PrecTimer ctimer;
  ttimer.reset();     // if you want to reset the timer (not needed here)
  ttimer.start();     // start the total timer
  for (int i=0; i<n; ++i) {
    ... do something ...
    ctimer.start();   // start the calc timer
    ...do some calculation which will be timed...
    ctimer.stop();    // and stop it
  }
  ttimer.stop();
  ttimer.show (cout, "Total       ");
  ctimer.show (cout, "Calculations");

Member Description

PrecTimer()

Construct.

~PrecTimer()

Destruct.

void start()

Restart the timer.

void stop()

Stop the timer

void reset()

Reset the timer to zero.

void show() const
void show (std::ostream& os) const

Show real time on cout or a user supplied stream.

void show (const String&) const
void show (std::ostream& os, const ostream& prefix) const

Show real time on cout or a user supplied stream preceeded by the string parameter.

double getReal() const

Get the real time (in seconds).

unsigned long long getCount() const

Get the total number of times start/stop is done.

void print_time (std::ostream&, double time) const

static double get_CPU_speed_in_MHz()