1#ifndef DUNE_PDELAB_COMMON_BENCHMARKHELPER_HH
2#define DUNE_PDELAB_COMMON_BENCHMARKHELPER_HH
20 struct CppClockWallTimeSource
23 double operator()()
const
25 return static_cast<double>(std::clock()) /
static_cast<double>(CLOCKS_PER_SEC);
33 struct MPIWallTimeSource
36 double operator()()
const
43 typedef MPIWallTimeSource DefaultTimeSource;
47 typedef CppClockWallTimeSource DefaultTimeSource;
56 double elapsed()
const
65 std::vector<Timing> timings;
72 template<
typename TimeSource = DefaultTimeSource>
73 struct BenchmarkHelper
76 BenchmarkHelper(std::string name, std::size_t max_runs = 1, TimeSource timeSource = TimeSource())
81 , _statistics_stale(true)
83 _run_times.timings.resize(max_runs);
89 if (_run >= _max_runs)
93 _statistics_stale =
true;
94 _run_times.timings[_run].start = _time();
97 void start_run(std::ostream& s)
100 ios_base_all_saver ios_saver(s);
101 s << _name <<
" (" << std::setw(2) << _run <<
" of " << std::setw(2) << _max_runs <<
") " << std::flush;
107 _run_times.timings[_run].end = _time();
111 void end_run(std::ostream& s)
114 ios_base_all_saver ios_saver(s);
115 s <<
" " << std::setw(10) << std::setprecision(3) << _run_times.timings[_run-1].elapsed() <<
" sec" << std::endl;
118 void start(std::string task)
121 std::map<std::string,BenchmarkEntry>::iterator,
123 > res = _tasks.insert(make_pair(task,BenchmarkEntry()));
125 res.first->second.timings.resize(_max_runs);
126 res.first->second.timings[_run].start = _time();
127 _statistics_stale =
true;
130 void start(std::string task, std::ostream& s)
135 void end(std::string task)
137 _tasks[task].timings[_run].end = _time();
138 _statistics_stale =
true;
141 void end(std::string task, std::ostream& s)
144 s <<
"." << std::flush;
147 void update_entry(BenchmarkEntry& entry)
154 for (std::vector<Timing>::iterator it = entry.timings.begin(), end = entry.timings.end();
158 const double elapsed = it->elapsed();
159 entry.min =
std::min(entry.min,elapsed);
160 entry.max =
std::max(entry.max,elapsed);
161 entry.avg += elapsed;
162 entry.std_dev += elapsed*elapsed;
165 entry.avg /= entry.timings.size();
166 entry.std_dev /= entry.timings.size();
167 entry.std_dev = std::sqrt(entry.std_dev - entry.avg*entry.avg);
170 void update_statistics()
173 for (std::map<std::string,BenchmarkEntry>::iterator it = _tasks.begin(), end = _tasks.end();
177 _max_name_len =
std::max(_max_name_len,it->first.size());
178 update_entry(it->second);
181 update_entry(_run_times);
183 _statistics_stale =
false;
186 void print_entry(std::ostream& s, std::string name,
const BenchmarkEntry& entry,
bool summary_only =
false)
const
188 s << std::setw(_max_name_len + 1) << std::left << name
189 << std::right << std::scientific << std::setw(10) << std::setprecision(2);
191 for (std::vector<Timing>::const_iterator it = entry.timings.begin(),
192 end = entry.timings.end();
196 s << std::setw(10) << it->elapsed();
198 s << std::setw(10) << entry.min
199 << std::setw(10) << entry.max
200 << std::setw(10) << entry.avg
201 << std::setw(10) << entry.std_dev;
206 void print(std::ostream& s,
bool summary_only =
false)
208 ios_base_all_saver ios_saver(s);
210 if (_statistics_stale)
213 s << _name <<
" (" << std::setw(2) << _run <<
" of " << std::setw(2) << _max_runs <<
") runs" << std::endl;
215 s << std::setw(_max_name_len + 1) <<
"";
218 for (std::size_t i = 0; i < _max_runs; ++i)
219 s << std::setw(10) << i;
221 s << std::setw(10) <<
"min"
222 << std::setw(10) <<
"max"
223 << std::setw(10) <<
"avg"
224 << std::setw(10) <<
"std_dev" << std::endl;
226 for (std::map<std::string,BenchmarkEntry>::const_iterator it = _tasks.begin(), end = _tasks.end();
229 print_entry(s,it->first,it->second,summary_only);
231 print_entry(s,
"total",_run_times,summary_only);
235 const std::string _name;
238 const std::size_t _max_runs;
239 std::map<std::string,BenchmarkEntry> _tasks;
240 bool _statistics_stale;
241 BenchmarkEntry _run_times;
242 std::size_t _max_name_len;
Default exception class for range errors.
Definition: exceptions.hh:254
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:484
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:506
Utility class for storing and resetting stream attributes.
Dune namespace.
Definition: alignedallocator.hh:13