next up previous contents index
Next: Counter ( counter ) Up: Simple Data Types and Previous: Some Useful Functions (   Contents   Index

Timer ( timer )


The class timer facilitates time measurements. An instance t has two states: running or stopped. It measures the time which elapses while it is in the state running. The state depends on a (non-negative) internal counter, which is incremented by every start operation and decremented by every stop operation. The timer is running iff the counter is not zero. The use of a counter (instead of a boolean flag) to determine the state is helpful when a recursive function f is measured, which is shown in the example below:

#include <LEDA/system/timer.h>
leda::timer f_timer;

void f()

  // do something ...
  f(); // recursive call
  // do something else ...

  f_timer.stop(); // timer is stopped when top-level call returns

int main()
  std::cout << "time spent in f " << f_timer << "\n"; return 0;

Let us analyze this example. When f is called in main, the timer is in the state stopped. The first start operation (in the top-level call) increments the counter from zero to one and puts the timer into the state running. In a recursive call the counter is incremented at the beginning and decremented upon termination, but the timer remains in the state running. Only when the top-level call of f terminates and returns to main, the counter is decremented from one to zero, which puts the timer into the state stopped. So the timer measures the total running time of f (including recursive calls).

#include < LEDA/system/timer.h >


timer::measure auxiliary class to facilitate measurements (see example below).


timer t(const string& name, bool report_on_destruction = true)
    creates an instance t with the given name. If report_on_destruction is true, then the timer reports upon its destruction how long it has been running in total. The initial state of the timer is stopped.

timer t creates an unnamed instance t and sets the report_on_destruction flag to false. The initial state of the timer is stopped.


void t.reset() sets the internal counter and the total elapsed time to zero.

void t.start() increments the internal counter.

void t.stop() decrements the internal counter. (If the counter is already zero, nothing happens.)

void t.restart() short-hand for t.reset() + t.start().

void t.halt() sets the counter to zero, which forces the timer into the state stopped no matter how many start operations have been executed before.

bool t.is_running() returns if t is currently in the state running.

float t.elapsed_time() returns how long (in seconds) t has been in the state running (since the last reset).

void t.set_name(const string& name)
    sets the name of t.

string t.get_name() returns the name of t.

void t.report_on_desctruction(bool do_report = true)
    sets the flag report_on_destruction to do_report.

bool t.will_report_on_desctruction()
    returns whether t will issue a report upon its destruction.


We give an example demonstrating the use of the class measure. Note that the function below has several return statements, so it would be tedious to stop the timer ``by hand''.

#include <LEDA/system/timer.h>

unsigned fibonacci(unsigned n)
  static leda::timer t("fibonacci");
    // report total time upon destruction of t

  leda::timer::measure m(t);
    // starts the timer t when m is constructed, and stops t
    // when m is destroyed, i.e. when the function returns

  if (n < 1) return 0;
  else if (n == 1) return 1;
  else return fibonacci(n-1) + fibonacci(n-2);

int main()
  std::cout << fibonacci(40) << "\n";
  return 0; // reports "Timer(fibonacci): X.XX s" upon termination

next up previous contents index
Next: Counter ( counter ) Up: Simple Data Types and Previous: Some Useful Functions (   Contents   Index