mCRL2
Loading...
Searching...
No Matches
execution_timer.h
Go to the documentation of this file.
1// Author(s): Jeroen Keiren
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
11
12#ifndef MCRL2_UTILITIES_EXECUTION_TIMER_H
13#define MCRL2_UTILITIES_EXECUTION_TIMER_H
14
16
17#include <chrono>
18#include <fstream>
19#include <map>
20#include <string>
21
22namespace mcrl2
23{
24
25namespace utilities
26{
27
47{
48 protected:
49
51 struct timing
52 {
53 std::chrono::steady_clock::time_point start;
54 std::chrono::steady_clock::time_point finish;
55
56 clock_t start_user;
57 clock_t finish_user;
58
60 start(),
61 finish()
62 {}
63 };
64
65 std::string m_tool_name;
66 std::string m_filename;
67 std::map<std::string, timing> m_timings;
68
71 void write_report(std::ostream& s)
72 {
73 std::ios::fmtflags oldflags = s.setf(std::ios::fixed, std::ios::floatfield);
74 s.precision(3);
75
76 s << "- tool: " << m_tool_name << std::endl
77 << " timing:" << std::endl;
78
79 for (std::map<std::string, timing>::const_iterator i = m_timings.begin(); i != m_timings.end(); ++i)
80 {
81 if (i->second.finish == std::chrono::steady_clock::time_point())
82 {
83 s << " " << i->first << ": did not finish. " << std::endl;
84 }
85 else if (i->second.start > i->second.finish)
86 {
87 throw mcrl2::runtime_error("Start of " + i->first + " occurred after finish.");
88 }
89 else
90 {
91 s << " " << i->first << ": "
92 << std::chrono::duration_cast<std::chrono::milliseconds>(i->second.finish - i->second.start).count() / 1000.0
93 << "s (user: " << static_cast<double>(i->second.finish_user - i->second.start_user) / CLOCKS_PER_SEC << "s)"
94 << std::endl;
95 }
96 }
97 s.flags(oldflags);
98 }
99
100 public:
101
105 execution_timer(const std::string& tool_name = "", std::string const& filename = "") :
106 m_tool_name(tool_name),
107 m_filename(filename)
108 {}
109
112 {}
113
118 void start(const std::string& timing_name)
119 {
120 std::map<std::string, timing>::iterator t = m_timings.lower_bound(timing_name);
121 if (t != m_timings.end() && t->first == timing_name)
122 {
123 throw mcrl2::runtime_error("Starting already known timing '" + timing_name + "'. This causes unreliable results.");
124 }
125 t = m_timings.insert(t, make_pair(timing_name, timing()));
126
127 t->second.start = std::chrono::steady_clock::now();
128 t->second.start_user = clock();
129 }
130
135 void finish(const std::string& timing_name)
136 {
137 std::chrono::steady_clock::time_point finish = std::chrono::steady_clock::now();
138 const std::map<std::string, timing>::iterator t = m_timings.find(timing_name);
139 if (t == m_timings.end())
140 {
141 throw mcrl2::runtime_error("Finishing timing '" + timing_name + "' that was not started.");
142 }
143 if (std::chrono::steady_clock::time_point() != t->second.finish)
144 {
145 throw mcrl2::runtime_error("Finishing timing '" + timing_name + "' for the second time.");
146 }
147
148 t->second.finish = finish;
149 t->second.finish_user = clock();
150 }
151
158 void report()
159 {
160 if (m_filename.empty())
161 {
162 write_report(std::cerr);
163 }
164 else
165 {
166 std::ofstream out;
167 out.open(m_filename.c_str(), std::ios::app);
168 write_report(out);
169 out.close();
170 }
171 }
172
173};
174
175} // namespace utilities
176
177} // namespace mcrl2
178
179#endif // MCRL2_UTILITIES_EXECUTION_TIMER_H
Standard exception class for reporting runtime errors.
Definition exception.h:27
Simple timer to time the CPU time used by a piece of code.
void write_report(std::ostream &s)
Write the report to an output stream.
std::map< std::string, timing > m_timings
collection of timings
void start(const std::string &timing_name)
Start measurement with a hint.
std::string m_filename
name of the file to write timings to
void finish(const std::string &timing_name)
Finish a measurement with a hint.
void report()
Write all timing information that has been recorded.
execution_timer(const std::string &tool_name="", std::string const &filename="")
Constructor of a simple execution timer.
std::string m_tool_name
name of the tool we are timing
Exception classes for use in libraries and tools.
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72
Pair of start and finish times.
std::chrono::steady_clock::time_point finish
std::chrono::steady_clock::time_point start