OpenVDB: CpuTimer.h Source File

OpenVDB 8.0.0

CpuTimer.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
4 #ifndef OPENVDB_UTIL_CPUTIMER_HAS_BEEN_INCLUDED
5 #define OPENVDB_UTIL_CPUTIMER_HAS_BEEN_INCLUDED
6
7 #include <openvdb/version.h>
8 #include <string>
9 #include <chrono>
10 #include <iostream>// for std::cerr
11 #include <sstream>// for ostringstream
12 #include <iomanip>// for setprecision
13 #include "Formats.h"// for printTime
14
15 namespace openvdb {
16 OPENVDB_USE_VERSION_NAMESPACE
17 namespace OPENVDB_VERSION_NAME {
18 namespace util {
19
66 class CpuTimer
67 {
68 public:
70 CpuTimer(std::ostream& os = std::cerr) : mOutStream(os), mT0(this->now()) {}
71
75 CpuTimer(const std::string& msg, std::ostream& os = std::cerr) : mOutStream(os) { this->start(msg); }
76
80 inline void start() { mT0 = this->now(); }
81
85 inline void start(const std::string& msg)
86 {
87 mOutStream << msg << " ...";
88 this->start();
89 }
90
94 inline int64_t microseconds() const
95 {
96 return (this->now() - mT0);
97 }
98
102 inline double milliseconds() const
103 {
104 static constexpr double resolution = 1.0 / 1E3;
105 return static_cast<double>(this->microseconds()) * resolution;
106 }
107
111 inline double seconds() const
112 {
113 static constexpr double resolution = 1.0 / 1E6;
114 return static_cast<double>(this->microseconds()) * resolution;
115 }
116
117 inline std::string time() const
118 {
119 const double msec = this->milliseconds();
120 std::ostringstream os;
121 printTime(os, msec, "", "", 4, 1, 1);
122 return os.str();
123 }
124
128 inline double stop() const
129 {
130 const double msec = this->milliseconds();
131 printTime(mOutStream, msec, " completed in ", "\n", 4, 3, 1);
132 return msec;
133 }
134
138 inline double stop(const std::string& msg) const
139 {
140 const double msec = this->milliseconds();
141 mOutStream << msg << " ...";
142 printTime(mOutStream, msec, " completed in ", "\n", 4, 3, 1);
143 return msec;
144 }
145
150 inline double restart()
151 {
152 const double msec = this->milliseconds();
153 this->start();
154 return msec;
155 }
156
161 inline double restart(const std::string& msg)
162 {
163 const double delta = this->stop();
164 this->start(msg);
165 return delta;
166 }
167
168 private:
169 static int64_t now()
170 {
171 // steady_clock is a monotonically increasing clock designed for timing duration
172 // note that high_resolution_clock is aliased to either steady_clock or system_clock
173 // depending on the platform, so it is preferrable to use steady_clock
174 const auto time_since_epoch =
175 std::chrono::steady_clock::now().time_since_epoch();
176 // cast time since epoch into microseconds (1 / 1000000 seconds)
177 const auto microseconds =
178 std::chrono::duration_cast<std::chrono::microseconds>(time_since_epoch).count();
179 // cast to a a 64-bit signed integer as this will overflow in 2262!
180 return static_cast<int64_t>(microseconds);
181 }
182
183 std::ostream& mOutStream;
184 int64_t mT0{0};
185 };// CpuTimer
186
187 } // namespace util
188 } // namespace OPENVDB_VERSION_NAME
189 } // namespace openvdb
190
191
192 #endif // OPENVDB_UTIL_CPUTIMER_HAS_BEEN_INCLUDED
openvdb::v8_2::util::printTime
OPENVDB_API int printTime(std::ostream &os, double milliseconds, const std::string &head="", const std::string &tail="\n", int width=4, int precision=1, int verbose=0)
openvdb::v8_2::util::CpuTimer::CpuTimer
CpuTimer(std::ostream &os=std::cerr)
Initiate timer.
Definition: CpuTimer.h:70
openvdb::v8_2::util::CpuTimer::restart
double restart()
Re-start timer.
Definition: CpuTimer.h:150
openvdb::v8_2::util::CpuTimer::start
void start(const std::string &msg)
Print message and start timer.
Definition: CpuTimer.h:85
openvdb::v8_2::util::CpuTimer::start
void start()
Start timer.
Definition: CpuTimer.h:80
version.h
Library and file format version numbers.
openvdb::v8_2::util::CpuTimer::milliseconds
double milliseconds() const
Return Time difference in milliseconds since construction or start was called.
Definition: CpuTimer.h:102
openvdb::v8_2::util::CpuTimer::stop
double stop() const
Returns and prints time in milliseconds since construction or start was called.
Definition: CpuTimer.h:128
openvdb::v8_2::util::CpuTimer
Simple timer for basic profiling.
Definition: CpuTimer.h:67
Formats.h
Utility routines to output nicely-formatted numeric values.
openvdb::v8_2::util::CpuTimer::time
std::string time() const
Definition: CpuTimer.h:117
openvdb::v8_2::util::CpuTimer::restart
double restart(const std::string &msg)
Stop previous timer, print message and re-start timer.
Definition: CpuTimer.h:161
openvdb::v8_2::util::CpuTimer::CpuTimer
CpuTimer(const std::string &msg, std::ostream &os=std::cerr)
Prints message and start timer.
Definition: CpuTimer.h:75
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:153
openvdb::v8_2::util::CpuTimer::microseconds
int64_t microseconds() const
Return Time difference in microseconds since construction or start was called.
Definition: CpuTimer.h:94
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:101
openvdb::v8_2::util::CpuTimer::seconds
double seconds() const
Return Time difference in seconds since construction or start was called.
Definition: CpuTimer.h:111
openvdb
Definition: openvdb/Exceptions.h:13
openvdb::v8_2::util::CpuTimer::stop
double stop(const std::string &msg) const
Returns and prints time in milliseconds since construction or start was called.
Definition: CpuTimer.h:138