OpenVDB  7.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 <tbb/tick_count.h>
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:
69 
71  CpuTimer(std::ostream& os = std::cerr) : mOutStream(os), mT0(tbb::tick_count::now()) {}
72 
76  CpuTimer(const std::string& msg, std::ostream& os = std::cerr) : mOutStream(os), mT0() { this->start(msg); }
77 
81  inline void start() { mT0 = tbb::tick_count::now(); }
82 
86  inline void start(const std::string& msg)
87  {
88  mOutStream << msg << " ...";
89  this->start();
90  }
91 
95  inline double milliseconds() const
96  {
97  tbb::tick_count::interval_t dt = tbb::tick_count::now() - mT0;
98  return 1000.0*dt.seconds();
99  }
100 
104  inline double seconds() const
105  {
106  tbb::tick_count::interval_t dt = tbb::tick_count::now() - mT0;
107  return dt.seconds();
108  }
109 
111  OPENVDB_DEPRECATED inline double delta() const { return this->milliseconds(); }
112 
113  inline std::string time() const
114  {
115  const double msec = this->milliseconds();
116  std::ostringstream os;
117  printTime(os, msec, "", "", 4, 1, 1);
118  return os.str();
119  }
120 
124  inline double stop() const
125  {
126  const double msec = this->milliseconds();
127  printTime(mOutStream, msec, " completed in ", "\n", 4, 3, 1);
128  return msec;
129  }
130 
134  inline double stop(const std::string& msg) const
135  {
136  const double msec = this->milliseconds();
137  mOutStream << msg << " ...";
138  printTime(mOutStream, msec, " completed in ", "\n", 4, 3, 1);
139  return msec;
140  }
141 
146  inline double restart()
147  {
148  const double msec = this->milliseconds();
149  this->start();
150  return msec;
151  }
152 
157  inline double restart(const std::string& msg)
158  {
159  const double delta = this->stop();
160  this->start(msg);
161  return delta;
162  }
163 
164 private:
165  std::ostream& mOutStream;
166  tbb::tick_count mT0;
167 };// CpuTimer
168 
169 } // namespace util
170 } // namespace OPENVDB_VERSION_NAME
171 } // namespace openvdb
172 
173 
174 #endif // OPENVDB_UTIL_CPUTIMER_HAS_BEEN_INCLUDED
#define OPENVDB_DEPRECATED
Definition: Platform.h:42
OPENVDB_DEPRECATED double delta() const
This method is identical to milliseconds() - deprecated.
Definition: CpuTimer.h:111
Utility routines to output nicely-formatted numeric values.
double milliseconds() const
Return Time difference in milliseconds since construction or start was called.
Definition: CpuTimer.h:95
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)
void start()
Start timer.
Definition: CpuTimer.h:81
double restart(const std::string &msg)
Stop previous timer, print message and re-start timer.
Definition: CpuTimer.h:157
CpuTimer(std::ostream &os=std::cerr)
Initiate timer.
Definition: CpuTimer.h:71
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
Definition: Exceptions.h:13
std::string time() const
Definition: CpuTimer.h:113
CpuTimer(const std::string &msg, std::ostream &os=std::cerr)
Prints message and start timer.
Definition: CpuTimer.h:76
Library and file format version numbers.
double stop() const
Returns and prints time in milliseconds since construction or start was called.
Definition: CpuTimer.h:124
double restart()
Re-start timer.
Definition: CpuTimer.h:146
Definition: Coord.h:16
double stop(const std::string &msg) const
Returns and prints time in milliseconds since construction or start was called.
Definition: CpuTimer.h:134
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
void start(const std::string &msg)
Print message and start timer.
Definition: CpuTimer.h:86
double seconds() const
Return Time difference in seconds since construction or start was called.
Definition: CpuTimer.h:104
Simple timer for basic profiling.
Definition: CpuTimer.h:66