![]() |
Cadabra
Computer algebra system for field theory problems
|
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; Stopwatch s;
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 << '
';
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; }
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... | |
typedef std::chrono::steady_clock Stopwatch::clock |
Stopwatch::Stopwatch | ( | ) |
|
private |
void Stopwatch::reset | ( | ) |
Reset to no-time-elapsed.
long Stopwatch::seconds | ( | ) | const |
Number of seconds elapsed.
void Stopwatch::start | ( | ) |
Continue timing (does not reset).
void Stopwatch::stop | ( | ) |
Stop timing.
bool Stopwatch::stopped | ( | ) | const |
Is the stopwatch currently timing?
long Stopwatch::useconds | ( | ) | const |
Number of micro-seconds elapsed (needs to be added to 'seconds').
|
friend |
Print human-readable representation of the time elapsed.
|
mutableprivate |
|
staticprivate |
|
mutableprivate |
|
private |