Cadabra
Computer algebra system for field theory problems
Public Types | Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
Stopwatch Class Reference

Description

The Stopwach class provides a simple interace to allow timing function calls etc...

It is possible to stop the stopwatch for an indefinite amount of time without losing the current elapsed time period, allowing it to be used to calculate the actual amount of time a function spends performing a task without taking into account e.g. time spent being blocked by a mutex. The class is exported to python in the cadabra2 module as Stopwatch.

Example C++ usage:

#include <iostream>
#include <vector>
#include "Stopwatch.hh"
void do_other_stuff() { std::cout << "Doing other stuff\n"; }
int main()
{
const int n = 1000;
std::vector<int> v;
s.start();
for (unsigned int i = 0; i < n; ++i)
v.push_back(i);
s.stop();
do_other_stuff();
s.start();
for (unsigned int i = n; i > 0; --i)
v.push_back(i);
s.stop();
std::cout << "Total time spent pushing to vector and not spent doing other stuff: " << s << '\n';
s.reset();
s.start();
do_other_stuff();
s.stop();
std::cout << "Doing other stuff takes " << s.useconds() << "us\n"; //Assuming do_other_stuff() takes < 1s
return 0;
}
int main(int, char **)
Definition: adjform.cc:7
The Stopwach class provides a simple interace to allow timing function calls etc.....
Definition: Stopwatch.hh:107
void reset()
Reset to no-time-elapsed.
Definition: Stopwatch.cc:29
void start()
Continue timing (does not reset).
Definition: Stopwatch.cc:35
void stop()
Stop timing.
Definition: Stopwatch.cc:41

Example python usage:

from cadabra2 import Stopwatch
def do_other_stuff():
print("Doing other stuff")
n = 1000
v = []
s = Stopwatch()
s.start()
for i in range(n):
v += [i]
s.stop()
do_other_stuff()
s.start()
for i in range(n, 0, -1):
v += [i]
s.stop()
print("Total time spent pushing to vector and not spent doing other stuff: {}".format(s))
s.reset()
s.start()
do_other_stuff()
s.stop()
print("Doing other stuff takes {}us".format(s.useconds())) #Assuming do_other_stuff() takes < 1s

#include <Stopwatch.hh>

Public Types

typedef std::chrono::steady_clock clock
 

Public Member Functions

 Stopwatch ()
 
void reset ()
 Reset to no-time-elapsed. More...
 
void start ()
 Continue timing (does not reset). More...
 
void stop ()
 Stop timing. More...
 
long seconds () const
 Number of seconds elapsed. More...
 
long useconds () const
 Number of micro-seconds elapsed (needs to be added to 'seconds'). More...
 
bool stopped () const
 Is the stopwatch currently timing? More...
 

Private Member Functions

void checkpoint_ () const
 

Private Attributes

clock::time_point start_
 
long elapsed_
 
bool stopped_
 

Static Private Attributes

static const long s_to_us = 1000000L
 

Friends

std::ostream & operator<< (std::ostream &, const Stopwatch &)
 Print human-readable representation of the time elapsed. More...
 

Member Typedef Documentation

◆ clock

typedef std::chrono::steady_clock Stopwatch::clock

Constructor & Destructor Documentation

◆ Stopwatch()

Stopwatch::Stopwatch ( )

Member Function Documentation

◆ checkpoint_()

void Stopwatch::checkpoint_ ( ) const
private

◆ reset()

void Stopwatch::reset ( )

Reset to no-time-elapsed.

◆ seconds()

long Stopwatch::seconds ( ) const

Number of seconds elapsed.

◆ start()

void Stopwatch::start ( )

Continue timing (does not reset).

◆ stop()

void Stopwatch::stop ( )

Stop timing.

◆ stopped()

bool Stopwatch::stopped ( ) const

Is the stopwatch currently timing?

◆ useconds()

long Stopwatch::useconds ( ) const

Number of micro-seconds elapsed (needs to be added to 'seconds').

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  ,
const Stopwatch  
)
friend

Print human-readable representation of the time elapsed.

Member Data Documentation

◆ elapsed_

long Stopwatch::elapsed_
mutableprivate

◆ s_to_us

const long Stopwatch::s_to_us = 1000000L
staticprivate

◆ start_

clock::time_point Stopwatch::start_
mutableprivate

◆ stopped_

bool Stopwatch::stopped_
private

The documentation for this class was generated from the following files: