blob: 5310b58b41eb88109b12dde4fe866fe48d8ef7e9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/**
** \file misc/timer.hh
** \brief timer: Timing nested tasks.
*/
#pragma once
#include <iosfwd>
#include <iostream>
#include <map>
#include <stack>
#include <string>
namespace misc
{
/// Timing nested tasks.
class timer
{
public:
timer();
timer(const timer& rhs);
~timer();
/// Start a sub timer for a named task.
/// \param name a constant string which is the task's name
void push(const std::string& name);
/// Start a sub timer with an integer corresponding to a task.
/// \see push()
/// \see name()
void push(int i);
/// Stop the current task and ensure its name matches the string
/// passed as argument.
/// \param task_name The name of the task to stop.
void pop(const std::string& task_name);
/// \see pop()
/// \see pop(const std::string name)
void pop(int i);
/// Stop the current task's timer(the last task pushed on the stack).
void pop();
/// Write results.
/// \param out An output stream, set to std::cerr by default.
void dump(std::ostream& out = std::cerr);
/// Enable automatic information dumping upon destruction of the
/// timer on stream \a out.
void dump_on_destruction(std::ostream& out);
/// Assign name \a task_name to task number \a i.
void name(int i, const std::string& task_name);
/// Start the timer.
/// \see stop()
void start();
/// Stop the timer.
/// \see start()
void stop();
/// \brief Import timer.
///
/// Import tasks defined in \a rhs. The total execution time of
/// \a rhs is ignored.
///
/// \pre No task should be running in \a rhs.
timer& operator<<(const timer& rhs);
private:
class time_var;
class time
{
friend class timer;
friend class timer::time_var;
public:
time();
time& operator+=(const time& rhs);
private:
long user;
long sys;
long wall;
};
class time_var
{
public:
time_var();
void start();
void stop();
bool is_zero();
time begin;
time elapsed;
time first;
time last;
bool initial;
};
/// Write formatted timing results on \a out.
void timeinfo(long time, long total_time, std::ostream& out);
using task_map_type = std::map<const std::string, time_var*>;
/// Time information associated to each task name.
task_map_type tasksmap;
/// Stack of timed tasks.
std::stack<time_var*> tasks;
/// Dictionnary mapping an integer to a task name.
/// \see push(int)
std::map<int, std::string> intmap;
/// Total time measured by the timer.
/// \see start()
/// \see stop()
time_var total;
/// A potential stream onto which results are dumped when the
/// timer is destroyed. If this pointer is null, no action is
/// taken during the destruction of the timer.
std::ostream* dump_stream;
/// Number of clocks ticks per second, set according to the system
/// timing function used.
static const long clocks_per_sec;
};
} // namespace misc
#include <misc/timer.hxx>
|