HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CpuTimer.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2017 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 <string>
35 #include <tbb/tick_count.h>
36 #include <sstream>// for ostringstream
37 #include <iomanip>//for setprecision
38 
39 namespace openvdb {
41 namespace OPENVDB_VERSION_NAME {
42 namespace util {
43 
44 /// @brief Simple timer for basic profiling.
45 ///
46 /// @code
47 /// CpuTimer timer;
48 /// // code here will not be timed!
49 /// timer.start("algorithm");
50 /// // code to be timed goes here
51 /// timer.stop();
52 /// @endcode
53 ///
54 /// or to time multiple blocks of code
55 ///
56 /// @code
57 /// CpuTimer timer("algorithm 1");
58 /// // code to be timed goes here
59 /// timer.restart("algorithm 2");
60 /// // code to be timed goes here
61 /// timer.stop();
62 /// @endcode
63 class CpuTimer
64 {
65 public:
66 
67  /// @brief Initiate timer
68  CpuTimer() : mT0(tbb::tick_count::now()) {}
69 
70  /// @brief Prints message and re-start timer.
71  ///
72  /// @note Should normally be followed by a call to stop()
73  CpuTimer(const std::string& msg) { this->start(msg); }
74 
75  /// @brief Start timer.
76  ///
77  /// @note Should normally be followed by a call to time()
78  inline void start() { mT0 = tbb::tick_count::now(); }
79 
80  /// @brief Print message and re-start timer.
81  ///
82  /// @note Should normally be followed by a call to stop()
83  inline void start(const std::string& msg)
84  {
85  std::cerr << msg << " ... ";
86  this->start();
87  }
88 
89  /// @brief Stop previous timer, print message and re-start timer.
90  ///
91  /// @note Should normally be followed by a call to stop()
92  inline void restart(const std::string& msg)
93  {
94  this->stop();
95  this->start(msg);
96  }
97 
98  /// Return Time diference in milliseconds since construction or start was called.
99  inline double delta() const
100  {
101  tbb::tick_count::interval_t dt = tbb::tick_count::now() - mT0;
102  return 1000.0*dt.seconds();
103  }
104 
105  /// @brief Print time in milliseconds since construction or start was called.
106  inline void stop() const
107  {
108  const double t = this->delta();
109  std::ostringstream ostr;
110  ostr << "completed in " << std::setprecision(3) << t << " ms\n";
111  std::cerr << ostr.str();
112  }
113 
114 private:
115 
116  tbb::tick_count mT0;
117 };// CpuTimer
118 
119 } // namespace util
120 } // namespace OPENVDB_VERSION_NAME
121 } // namespace openvdb
122 
123 
124 #endif // OPENVDB_UTIL_CPUTIMER_HAS_BEEN_INCLUDED
125 
126 // Copyright (c) 2012-2017 DreamWorks Animation LLC
127 // All rights reserved. This software is distributed under the
128 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
void stop() const
Print time in milliseconds since construction or start was called.
Definition: CpuTimer.h:106
void restart(const std::string &msg)
Stop previous timer, print message and re-start timer.
Definition: CpuTimer.h:92
double delta() const
Return Time diference in milliseconds since construction or start was called.
Definition: CpuTimer.h:99
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Simple timer for basic profiling.
Definition: CpuTimer.h:63
CpuTimer(const std::string &msg)
Prints message and re-start timer.
Definition: CpuTimer.h:73
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
void start(const std::string &msg)
Print message and re-start timer.
Definition: CpuTimer.h:83