Definition
The class counter can be used during profiling to count how often certain code is executed. An example is given below.
#include < LEDA/system/counter.h >
Creation
counter | c(const string& name, bool report_on_destruction = true) | |
creates an instance c with the given name. If report_on_destruction is true, then the counter reports its value upon destruction. The initial value of the counter is zero. | ||
counter | c | creates an unnamed instance c and sets the report_on_destruction flag to false. The initial value of the counter is zero. |
Operations
void | c.reset() | sets the value of c to zero. |
void | c.set_value(const unsigned long val) | |
sets the value of c to val. | ||
const unsigned long | c.get_value() | returns the current value of c. |
const unsigned long | c.increment() | increments c and returns its new value. (We also provide the operator ++.) |
void | c.set_name(const string& name) | |
sets the name of c. | ||
string | c.get_name() | returns the name of c. |
void | c.report_on_desctruction(bool do_report = true) | |
sets the flag report_on_destruction to do_report. | ||
bool | c.will_report_on_desctruction() | |
returns whether c will issue a report upon its destruction. |
Example
In the example below we count how often the function fibonacci is executed.
#include <LEDA/system/counter.h> unsigned fibonacci(unsigned n) { static leda::counter cnt("fibonacci"); // report upon destruction of cnt ++cnt; 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 "Counter(fibonacci) = 331160281" upon termination }