OpenVDB  7.2.1
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 {
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 
118  OPENVDB_DEPRECATED inline double delta() const { return this->milliseconds(); }
119 
120  inline std::string time() const
121  {
122  const double msec = this->milliseconds();
123  std::ostringstream os;
124  printTime(os, msec, "", "", 4, 1, 1);
125  return os.str();
126  }
127 
131  inline double stop() const
132  {
133  const double msec = this->milliseconds();
134  printTime(mOutStream, msec, " completed in ", "\n", 4, 3, 1);
135  return msec;
136  }
137 
141  inline double stop(const std::string& msg) const
142  {
143  const double msec = this->milliseconds();
144  mOutStream << msg << " ...";
145  printTime(mOutStream, msec, " completed in ", "\n", 4, 3, 1);
146  return msec;
147  }
148 
153  inline double restart()
154  {
155  const double msec = this->milliseconds();
156  this->start();
157  return msec;
158  }
159 
164  inline double restart(const std::string& msg)
165  {
166  const double delta = this->stop();
167  this->start(msg);
168  return delta;
169  }
170 
171 private:
172  static int64_t now()
173  {
174  // steady_clock is a monotonically increasing clock designed for timing duration
175  // note that high_resolution_clock is aliased to either steady_clock or system_clock
176  // depending on the platform, so it is preferrable to use steady_clock
177  const auto time_since_epoch =
178  std::chrono::steady_clock::now().time_since_epoch();
179  // cast time since epoch into microseconds (1 / 1000000 seconds)
180  const auto microseconds =
181  std::chrono::duration_cast<std::chrono::microseconds>(time_since_epoch).count();
182  // cast to a a 64-bit signed integer as this will overflow in 2262!
183  return static_cast<int64_t>(microseconds);
184  }
185 
186  std::ostream& mOutStream;
187  int64_t mT0{0};
188 };// CpuTimer
189 
190 } // namespace util
191 } // namespace OPENVDB_VERSION_NAME
192 } // namespace openvdb
193 
194 
195 #endif // OPENVDB_UTIL_CPUTIMER_HAS_BEEN_INCLUDED
openvdb::v7_2::util::CpuTimer::CpuTimer
CpuTimer(std::ostream &os=std::cerr)
Initiate timer.
Definition: CpuTimer.h:70
openvdb::v7_2::util::CpuTimer::restart
double restart()
Re-start timer.
Definition: CpuTimer.h:153
openvdb::v7_2::util::CpuTimer::start
void start()
Start timer.
Definition: CpuTimer.h:80
openvdb::v7_2::util::CpuTimer::milliseconds
double milliseconds() const
Return Time difference in milliseconds since construction or start was called.
Definition: CpuTimer.h:102
openvdb::v7_2::util::CpuTimer::stop
double stop() const
Returns and prints time in milliseconds since construction or start was called.
Definition: CpuTimer.h:131
openvdb::v7_2::util::CpuTimer::start
void start(const std::string &msg)
Print message and start timer.
Definition: CpuTimer.h:85
version.h
Library and file format version numbers.
openvdb::v7_2::util::CpuTimer::restart
double restart(const std::string &msg)
Stop previous timer, print message and re-start timer.
Definition: CpuTimer.h:164
Formats.h
Utility routines to output nicely-formatted numeric values.
OPENVDB_DEPRECATED
#define OPENVDB_DEPRECATED
Definition: Platform.h:42
openvdb::v7_2::util::CpuTimer::CpuTimer
CpuTimer(const std::string &msg, std::ostream &os=std::cerr)
Prints message and start timer.
Definition: CpuTimer.h:75
openvdb::v7_2::util::CpuTimer::time
std::string time() const
Definition: CpuTimer.h:120
openvdb::v7_2::util::CpuTimer::delta
OPENVDB_DEPRECATED double delta() const
This method is identical to milliseconds() - deprecated.
Definition: CpuTimer.h:118
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:147
openvdb::v7_2::util::CpuTimer::seconds
double seconds() const
Return Time difference in seconds since construction or start was called.
Definition: CpuTimer.h:111
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:95
openvdb::v7_2::util::CpuTimer
Simple timer for basic profiling.
Definition: CpuTimer.h:67
openvdb::v7_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:141
openvdb
Definition: openvdb/Exceptions.h:13
openvdb::v7_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::v7_2::util::CpuTimer::microseconds
int64_t microseconds() const
Return Time difference in microseconds since construction or start was called.
Definition: CpuTimer.h:94