HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CpuTimer.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
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 //
29 ///////////////////////////////////////////////////////////////////////////
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 
46 /// @brief Simple timer for basic profiling.
47 ///
48 /// @code
49 /// CpuTimer timer;
50 /// // code here will not be timed!
51 /// timer.start("algorithm");
52 /// // code to be timed goes here
53 /// timer.stop();
54 /// @endcode
55 ///
56 /// or to time multiple blocks of code
57 ///
58 /// @code
59 /// CpuTimer timer("algorithm 1");
60 /// // code to be timed goes here
61 /// timer.restart("algorithm 2");
62 /// // code to be timed goes here
63 /// timer.stop();
64 /// @endcode
65 class CpuTimer
66 {
67 public:
68 
69  /// @brief Initiate timer
70  CpuTimer() : mT0(tbb::tick_count::now()) {}
71 
72  /// @brief Prints message and re-start timer.
73  ///
74  /// @note Should normally be followed by a call to stop()
75  CpuTimer(const std::string& msg) { this->start(msg); }
76 
77  /// @brief Start timer.
78  ///
79  /// @note Should normally be followed by a call to time()
80  inline void start() { mT0 = tbb::tick_count::now(); }
81 
82  /// @brief Print message and re-start timer.
83  ///
84  /// @note Should normally be followed by a call to stop()
85  inline void start(const std::string& msg)
86  {
87  std::cerr << msg << " ... ";
88  this->start();
89  }
90 
91  /// @brief Stop previous timer, print message and re-start timer.
92  ///
93  /// @note Should normally be followed by a call to stop()
94  inline void restart(const std::string& msg)
95  {
96  this->stop();
97  this->start(msg);
98  }
99 
100  /// Return Time diference in milliseconds since construction or start was called.
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 
107  /// @brief Print time in milliseconds since construction or start was called.
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/ )
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:189
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
double delta() const
Return Time diference in milliseconds since construction or start was called.
Definition: CpuTimer.h:101
Simple timer for basic profiling.
Definition: CpuTimer.h:65
CpuTimer(const std::string &msg)
Prints message and re-start timer.
Definition: CpuTimer.h:75
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:135
void start(const std::string &msg)
Print message and re-start timer.
Definition: CpuTimer.h:85