TBTK
Need a break? Support the development by playing Polarity Puzzles
TBTK::Timer Class Reference

A Timer for measuring execution time. More...

#include <Timer.h>

Static Public Member Functions

static void tick (std::string tag="")
 
static void tock ()
 
static unsigned int createAccumulator (const std::string &tag="")
 
static void tick (unsigned int id)
 
static void tock (unsigned int id)
 
static void resetAccumulator (unsigned int id)
 
static void resetAccumulators ()
 
static void printAccumulators ()
 

Detailed Description

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.

Timer::tick("MyTag");
//Code
//...

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 "TBTK/Timer.h"
#include "TBTK/Streams.h"
#include "TBTK/TBTK.h"
#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(){
Timer::tick("Both functionA() and functionB()");
Timer::tick("functionA()");
functionA();
Timer::tick("functionB()");
functionA();
}
void accumulators(){
unsigned int idA = Timer::createAccumulator("functionA()");
unsigned int idB = Timer::createAccumulator("functionB()");
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()
===============================================================================

Member Function Documentation

◆ createAccumulator()

unsigned int TBTK::Timer::createAccumulator ( const std::string &  tag = "")
inlinestatic

Create an accumulator that can be used to accumulate multiple time measurements.

Parameters
tagOptional identifier tag that will be printed together with the accumulated time.
Returns
An id that can be passed to tick-tock calls to use the accumulator.

◆ printAccumulators()

void TBTK::Timer::printAccumulators ( )
inlinestatic

Print accumulators.

◆ resetAccumulator()

void TBTK::Timer::resetAccumulator ( unsigned int  id)
inlinestatic

Reset accumulator.

Parameters
idThe ID of the accumulator to reset.

◆ resetAccumulators()

void TBTK::Timer::resetAccumulators ( )
inlinestatic

Reset all accumulators.

◆ tick() [1/2]

void TBTK::Timer::tick ( std::string  tag = "")
inlinestatic

Push timestamp onto stack.

Parameters
tagOptional identifier tag that will be printed together with the elapsed time at subsequent tock call.

◆ tick() [2/2]

void TBTK::Timer::tick ( unsigned int  id)
inlinestatic

Initiate a time interval to be added to an accumulator.

Parameters
idThe ID of the accumulator to use.

◆ tock() [1/2]

void TBTK::Timer::tock ( )
inlinestatic

Pop timestamp from stack and print elapsed time and identifier tag.

◆ tock() [2/2]

void TBTK::Timer::tock ( unsigned int  id)
inlinestatic

Finalize a time interval and add it to an accumulator.

Parameters
idThe ID of the accumulator to stop.

The documentation for this class was generated from the following file:
TBTK::Timer::printAccumulators
static void printAccumulators()
Definition: Timer.h:231
Streams.h
Streams for TBTK output.
TBTK::Timer::tock
static void tock()
Definition: Timer.h:139
TBTK::Initialize
void Initialize()
Timer.h
A Timer for measuring execution time.
TBTK::Timer::tick
static void tick(std::string tag="")
Definition: Timer.h:134
TBTK::Timer::createAccumulator
static unsigned int createAccumulator(const std::string &tag="")
Definition: Timer.h:174
TBTK.h
TBTK initialization.