Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
Util.cxx
Go to the documentation of this file.
1#include "Math/Util.h"
2
3ROOT::Math::Util::TimingScope::TimingScope(std::function<void(std::string const &)> printer, std::string const &message)
4 : fBegin{std::chrono::steady_clock::now()}, fPrinter{printer}, fMessage{message}
5{
6}
7
8namespace {
9
10template <class T>
11std::string printTime(T duration)
12{
13 std::stringstream ss;
14 // Here, nanoseconds are represented as "long int", so the maximum value
15 // corresponds to about 300 years. Nobody will wait that long for a
16 // computation to complete, so for timing measurements it's fine to cast to
17 // nanoseconds to keep things simple.
18 double ns = std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count();
19 bool forceSeconds = false;
20 // The largest unit that we consider for pretty-printing is days.
21 if (ns >= 24 * 60 * 60e9) {
22 auto days = std::floor(ns / (24 * 60 * 60e9));
23 ss << days << " d ";
24 ns -= days * 24 * 60 * 60e9; // subtract the days to print only the rest
25 // to avoid printouts like "1 d 4 h 23 min 324 ns", we force to print
26 // seconds if we print either days, hours, or minutes
27 forceSeconds = true;
28 }
29 if (ns >= 60 * 60e9) {
30 auto hours = std::floor(ns / (60 * 60e9));
31 ss << hours << " h ";
32 ns -= hours * 60 * 60e9;
33 forceSeconds = true;
34 }
35 if (ns >= 60e9) {
36 auto minutes = std::floor(ns / 60e9);
37 ss << minutes << " min ";
38 ns -= minutes * 60e9;
39 forceSeconds = true;
40 }
41 if (ns >= 1e9 || forceSeconds) {
42 ss << (1e-9 * ns) << " s";
43 } else if (ns >= 1e6) {
44 ss << (1e-6 * ns) << " ms";
45 } else if (ns >= 1e3) {
46 ss << (1e-3 * ns) << " μs";
47 } else {
48 ss << ns << " ns";
49 }
50 return ss.str();
51}
52
53} // namespace
54
56{
57 using std::chrono::steady_clock;
58 steady_clock::time_point end = steady_clock::now();
59 std::stringstream ss;
60 ss << fMessage << " " << printTime(end - fBegin);
61 fPrinter(ss.str());
62}
#define e(i)
Definition RSha256.hxx:103
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
TimingScope(std::function< void(std::string const &)> printer, std::string const &message)
Definition Util.cxx:3