OpenVDB  5.2.0
CpuTimer.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2018 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 
41 namespace openvdb {
43 namespace OPENVDB_VERSION_NAME {
44 namespace util {
45 
65 class CpuTimer
66 {
67 public:
68 
70  CpuTimer() : mT0(tbb::tick_count::now()) {}
71 
75  CpuTimer(const std::string& msg) { this->start(msg); }
76 
80  inline void start() { mT0 = tbb::tick_count::now(); }
81 
85  inline void start(const std::string& msg)
86  {
87  std::cerr << msg << " ... ";
88  this->start();
89  }
90 
94  inline void restart(const std::string& msg)
95  {
96  this->stop();
97  this->start(msg);
98  }
99 
101  inline double delta() const
102  {
103  tbb::tick_count::interval_t dt = tbb::tick_count::now() - mT0;
104  return 1000.0*dt.seconds();
105  }
106 
108  inline void stop() const
109  {
110  const double t = this->delta();
111  std::ostringstream ostr;
112  ostr << "completed in " << std::setprecision(3) << t << " ms\n";
113  std::cerr << ostr.str();
114  }
115 
116 private:
117 
118  tbb::tick_count mT0;
119 };// CpuTimer
120 
121 } // namespace util
122 } // namespace OPENVDB_VERSION_NAME
123 } // namespace openvdb
124 
125 
126 #endif // OPENVDB_UTIL_CPUTIMER_HAS_BEEN_INCLUDED
127 
128 // Copyright (c) 2012-2018 DreamWorks Animation LLC
129 // All rights reserved. This software is distributed under the
130 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
Simple timer for basic profiling.
Definition: CpuTimer.h:65
double delta() const
Return Time diference in milliseconds since construction or start was called.
Definition: CpuTimer.h:101
CpuTimer()
Initiate timer.
Definition: CpuTimer.h:70
void start(const std::string &msg)
Print message and re-start timer.
Definition: CpuTimer.h:85
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
Definition: Exceptions.h:40
void stop() const
Print time in milliseconds since construction or start was called.
Definition: CpuTimer.h:108
void restart(const std::string &msg)
Stop previous timer, print message and re-start timer.
Definition: CpuTimer.h:94
Definition: Coord.h:42
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
void start()
Start timer.
Definition: CpuTimer.h:80
CpuTimer(const std::string &msg)
Prints message and re-start timer.
Definition: CpuTimer.h:75