A Timer for measuring execution time.
Modes
The Timer has two different modes of execution and it is possible to use these two modes at the same time.
Timestamp stack
A pair of tick-tock calls (with an optional tag as argument to the tick call) can be used to measure the time taken to execute a code segment.
The tick call pushes a timestamp and tag onto a stack, which is poped by the tock call. It is therefore possible to nest tick-tock calls to simultaneously measure the execution time for a large block of code and its smaller sections. When the timestamp is poped, the time since the tick call and the corresponding tag is printed to Streams::out.
Accumulators
An accumulator can be created using
unsigned int id = Timer::creteAccumulator("AccumulatorTag");
If the ID is passed to the tick and tock calls, the time between the calls is added to a corresponding accumulator. This can be used to measure the total time required to execute a specific code segment that for example is executed inside a loop.
To print the time accumulated in the accumulators, we use
Example
#include <complex>
using namespace std;
using namespace TBTK;
void functionA(){
for(unsigned int n = 0; n < 100; n++)
;
}
void functionB(){
for(unsigned int n = 0; n < 200; n++)
;
}
void timestampStack(){
functionA();
functionA();
}
void accumulators(){
for(unsigned int n = 0; n < 100; n++){
functionA();
functionB();
}
}
int main(){
timestampStack();
accumulators();
}
Output
(1) 38us 583ns functionA()
(1) 292ns functionB()
(0) 560us 459ns Both functionA() and functionB()
============================== Accumulator table ==============================
ID Time Tag
[0] 81us 334ns functionA()
[1] 2us 209ns functionB()
===============================================================================