TBTK
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) 587ns functionA()
(1) 127ns functionB()
(0) 18us 124ns Both functionA() and functionB()
============================== Accumulator table ==============================
ID Time Tag
[0] 3us 732ns functionA()
[1] 3us 594ns 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: