OpenVDB: CpuTimer.h Source File

OpenVDB 6.1.0

CpuTimer.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2019 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30
31 #ifndef OPENVDB_UTIL_CPUTIMER_HAS_BEEN_INCLUDED
32 #define OPENVDB_UTIL_CPUTIMER_HAS_BEEN_INCLUDED
33
34 #include <openvdb/version.h>
35 #include <string>
36 #include <tbb/tick_count.h>
37 #include <iostream>// for std::cerr
38 #include <sstream>// for ostringstream
39 #include <iomanip>// for setprecision
40 #include "Formats.h"// for printTime
41
42 namespace openvdb {
43 OPENVDB_USE_VERSION_NAMESPACE
44 namespace OPENVDB_VERSION_NAME {
45 namespace util {
46
93 class CpuTimer
94 {
95 public:
96
98 CpuTimer(std::ostream& os = std::cerr) : mOutStream(os), mT0(tbb::tick_count::now()) {}
99
103 CpuTimer(const std::string& msg, std::ostream& os = std::cerr) : mOutStream(os), mT0() { this->start(msg); }
104
108 inline void start() { mT0 = tbb::tick_count::now(); }
109
113 inline void start(const std::string& msg)
114 {
115 mOutStream << msg << " ...";
116 this->start();
117 }
118
122 inline double milliseconds() const
123 {
124 tbb::tick_count::interval_t dt = tbb::tick_count::now() - mT0;
125 return 1000.0*dt.seconds();
126 }
127
131 inline double seconds() const
132 {
133 tbb::tick_count::interval_t dt = tbb::tick_count::now() - mT0;
134 return dt.seconds();
135 }
136
138 OPENVDB_DEPRECATED inline double delta() const { return this->milliseconds(); }
139
140 inline std::string time() const
141 {
142 const double msec = this->milliseconds();
143 std::ostringstream os;
144 printTime(os, msec, "", "", 4, 1, 1);
145 return os.str();
146 }
147
151 inline double stop() const
152 {
153 const double msec = this->milliseconds();
154 printTime(mOutStream, msec, " completed in ", "\n", 4, 3, 1);
155 return msec;
156 }
157
161 inline double stop(const std::string& msg) const
162 {
163 const double msec = this->milliseconds();
164 mOutStream << msg << " ...";
165 printTime(mOutStream, msec, " completed in ", "\n", 4, 3, 1);
166 return msec;
167 }
168
173 inline double restart()
174 {
175 const double msec = this->milliseconds();
176 this->start();
177 return msec;
178 }
179
184 inline double restart(const std::string& msg)
185 {
186 const double delta = this->stop();
187 this->start(msg);
188 return delta;
189 }
190
191 private:
192 std::ostream& mOutStream;
193 tbb::tick_count mT0;
194 };// CpuTimer
195
196 } // namespace util
197 } // namespace OPENVDB_VERSION_NAME
198 } // namespace openvdb
199
200
201 #endif // OPENVDB_UTIL_CPUTIMER_HAS_BEEN_INCLUDED
202
203 // Copyright (c) 2012-2019 DreamWorks Animation LLC
204 // All rights reserved. This software is distributed under the
205 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
OPENVDB_DEPRECATED
#define OPENVDB_DEPRECATED
Definition: Platform.h:69
openvdb::v6_1::util::CpuTimer::delta
OPENVDB_DEPRECATED double delta() const
This method is identical to milliseconds() - deprecated.
Definition: CpuTimer.h:138
Formats.h
Utility routines to output nicely-formatted numeric values.
openvdb::v6_1::util::CpuTimer::stop
double stop() const
Returns and prints time in milliseconds since construction or start was called.
Definition: CpuTimer.h:151
openvdb::v6_1::util::CpuTimer::restart
double restart()
Re-start timer.
Definition: CpuTimer.h:173
openvdb::v6_1::util::CpuTimer::time
std::string time() const
Definition: CpuTimer.h:140
openvdb::v6_1::util::CpuTimer::restart
double restart(const std::string &msg)
Stop previous timer, print message and re-start timer.
Definition: CpuTimer.h:184
openvdb::v6_1::util::CpuTimer::start
void start(const std::string &msg)
Print message and start timer.
Definition: CpuTimer.h:113
openvdb::v6_1::util::CpuTimer::seconds
double seconds() const
Return Time difference in seconds since construction or start was called.
Definition: CpuTimer.h:131
openvdb::v6_1::util::CpuTimer::CpuTimer
CpuTimer(const std::string &msg, std::ostream &os=std::cerr)
Prints message and start timer.
Definition: CpuTimer.h:103
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:125
openvdb::v6_1::util::CpuTimer
Simple timer for basic profiling.
Definition: CpuTimer.h:93
openvdb
Definition: Exceptions.h:40
openvdb::v6_1::util::CpuTimer::milliseconds
double milliseconds() const
Return Time difference in milliseconds since construction or start was called.
Definition: CpuTimer.h:122
version.h
Library and file format version numbers.
openvdb::v6_1::util::CpuTimer::CpuTimer
CpuTimer(std::ostream &os=std::cerr)
Initiate timer.
Definition: CpuTimer.h:98
openvdb::v6_1::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::v6_1::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:161
tbb
Definition: Coord.h:43
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:177
openvdb::v6_1::util::CpuTimer::start
void start()
Start timer.
Definition: CpuTimer.h:108