Timing
Contents
Timing
The following variables, functions and classes are all defined within the namespace HIPP.
Ticker
-
class Ticker
Tickeris used for timing and recording events, and producing summary statistics of them.Tickeris copyable and movable. The copy is deep - new object is independent to the old object that gets copied. The move is destructive - old object cannot be used after being moved.-
typedef std::chrono::steady_clock clock_t
-
typedef clock_t::time_point point_t
-
typedef std::chrono::duration<double> dur_t
-
typedef std::size_t index_t
Typedefs.
clock_t: clock type for timing.point_t: time point used internally by the clock.dur_t: duration of event used by Ticker.index_t: index of the recorded events.
-
struct record_t
Type used by Ticker to record each event.
-
struct summary_t
Summary information of all recorded events.
-
Ticker()
Constructor - the internal time point is set to the time point of the construction.
-
void tick(int add_entry = 1)
-
void tick(const string &s)
Update the internal time point.
- Parameters
add_entry – If not
false, leave a record whose duration is the time elapsed since the last updating of internal time point.
The second overload is equivalent to set
add_entry = 1and leave an additional informationsfor the record.
-
double duration(int update = 1)
Return the time elapsed in seconds since the last update of the internal clock.
- Parameters
update – if
true, update the internal clock.
-
const point_t &last_time_point() const
-
static point_t now()
last_time_point(): get the last-updated time point of the internal clock.now(): get the time point of now.
-
record_t &query(index_t index)
-
record_t &query_last()
-
vector<record_t> &query_all() noexcept
-
const record_t &query(index_t index) const
-
const record_t &query_last() const
-
const vector<record_t> &query_all() const noexcept
Visit the recorded event.
query(index)- query event with givenindex.query_last()- query the last event.query_all()- get all records.
query()andquery_last()throw on illegal access.
-
summary_t summary() const noexcept
Return a printable summary instance for statistics.
Usage is like:
Ticker tk; cout << tk.summary() << endl;
-
ostream &info(ostream &os = cout, int fmt_cntl = 1) const
-
friend ostream &operator<<(ostream &os, const Ticker &ticker)
Print the ticker.
- Parameters
fmt_cntl –
0for a small, inline information and otherwise for a more detailed information of all records and their summary statistics.
operator<<()is equivalent toinfo()with the defaultfmt_cntl.
Example:
The following codes define a
Tickerinstance, use it to record events, and print the summary statistics:Ticker tk; for(int i=0; i<8; ++i){ tk.tick(0); // Perform some time-consuming computation, e.g., sleep(1); tk.tick(str("Task ", i)); } cout << tk.summary() << endl;
Output:
8 records, duration 1.00041 +/- 2.94286e-08
If more detailed information is needed, directly print the ticker:
cout << tk;
Output:
Ticker instance [loc=0x7ffcb94d97f0, size=32, align=8] ---------- 8 records, duration 1.00041 +/- 2.94286e-08 0: 1.00042, Task 0 1: 1.00038, Task 1 2: 1.00057, Task 2 3: 1.00033, Task 3 4: 1.00036, Task 4 5: 1.00077, Task 5 6: 1.00017, Task 6 7: 1.00028, Task 7
-
typedef std::chrono::steady_clock clock_t