FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
stop_watch.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
9#include <kernel/util/time_stamp.hpp>
10
11namespace FEAT
12{
21 {
22 private:
23 TimeStamp _start_time;
24 TimeStamp _stop_time;
25 bool _running;
26 long long _micros;
27
28 public:
29 StopWatch() :
30 _running(false),
31 _micros(0ll)
32 {
33 }
34
36 void reset()
37 {
38 _running = false;
39 _micros = 0ll;
40 }
41
43 void start()
44 {
45 if(!_running)
46 _start_time.stamp();
47 _running = true;
48 }
49
51 void stop()
52 {
53 if(_running)
54 {
55 _stop_time.stamp();
56
57 // update elapsed time
58 _micros += _stop_time.elapsed_micros(_start_time);
59 }
60 _running = false;
61 }
62
64 bool running() const
65 {
66 return _running;
67 }
68
70 double elapsed() const
71 {
72 return 1E-6 * double(elapsed_micros());
73 }
74
76 long long elapsed_micros() const
77 {
78 if(!_running)
79 return _micros;
80 else
81 {
82 // stop-watch is currently running, so compute current time
83 return _micros + TimeStamp().elapsed_micros(_start_time);
84 }
85 }
86
99 {
101 }
102
117 String percent_of(const StopWatch& total_watch, const String& prefix = " [", const String& postfix = "% ]") const
118 {
119 return prefix + stringify_fp_fix(100.0 * this->elapsed() / total_watch.elapsed(), 3, 7) + postfix;
120 }
121 }; // class StopWatch
122} // namespace FEAT
Stop-Watch class.
Definition: stop_watch.hpp:21
String percent_of(const StopWatch &total_watch, const String &prefix=" [", const String &postfix="% ]") const
Returns the formatted percentage that this stop watch elapsed time relative to another total runtime ...
Definition: stop_watch.hpp:117
double elapsed() const
Returns the total elapsed time in seconds.
Definition: stop_watch.hpp:70
void start()
Starts the stop-watch.
Definition: stop_watch.hpp:43
long long elapsed_micros() const
Returns the total elapsed time in micro-seconds.
Definition: stop_watch.hpp:76
void reset()
Resets the elapsed time.
Definition: stop_watch.hpp:36
void stop()
Stops the stop-watch and increments elapsed time.
Definition: stop_watch.hpp:51
String elapsed_string(TimeFormat format=TimeFormat::s_m) const
Return the time elapsed in the stop-watch.
Definition: stop_watch.hpp:98
bool running() const
Returns true if the stop-watch is currently running.
Definition: stop_watch.hpp:64
String class implementation.
Definition: string.hpp:46
Time stamp class.
Definition: time_stamp.hpp:54
static String format_micros(long long micros, TimeFormat format=TimeFormat::s_m)
Formats an elapsed time in microseconds as a string.
Definition: time_stamp.hpp:187
TimeStamp & stamp()
Stamps the current time-stamp.
Definition: time_stamp.hpp:79
long long elapsed_micros(const TimeStamp &before) const
Calculate the time elapsed between two time stamps in microseconds.
Definition: time_stamp.hpp:135
FEAT namespace.
Definition: adjactor.hpp:12
String stringify_fp_fix(DataType_ value, int precision=0, int width=0, bool sign=false)
Prints a floating point value to a string in fixed-point notation.
Definition: string.hpp:1142
TimeFormat
Definition: time_stamp.hpp:31