OpenVDB 3.2.0

CpuTimer.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2016 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 <string>
35 #include <tbb/tick_count.h>
36 #include <sstream>// for ostringstream
37 #include <iomanip>//for setprecision
38
39 namespace openvdb {
40 OPENVDB_USE_VERSION_NAMESPACE
41 namespace OPENVDB_VERSION_NAME {
42 namespace util {
43
63 class CpuTimer
64 {
65 public:
66
68 CpuTimer() : mT0(tbb::tick_count::now()) {}
69
73 CpuTimer(const std::string& msg) { this->start(msg); }
74
78 inline void start() { mT0 = tbb::tick_count::now(); }
79
83 inline void start(const std::string& msg)
84 {
85 std::cerr << msg << " ... ";
86 this->start();
87 }
88
92 inline void restart(const std::string& msg)
93 {
94 this->stop();
95 this->start(msg);
96 }
97
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
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-2016 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/ )
openvdb::v3_2_0::util::CpuTimer::start
void start(const std::string &msg)
Print message and re-start timer.
Definition: CpuTimer.h:83
openvdb::v3_2_0::util::CpuTimer
Simple timer for basic profiling.
Definition: CpuTimer.h:63
openvdb::v3_2_0::util::CpuTimer::delta
double delta() const
Return Time diference in milliseconds since construction or start was called.
Definition: CpuTimer.h:99
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:43
openvdb
Definition: Exceptions.h:39
openvdb::v3_2_0::util::CpuTimer::start
void start()
Start timer.
Definition: CpuTimer.h:78
openvdb::v3_2_0::util::CpuTimer::CpuTimer
CpuTimer()
Initiate timer.
Definition: CpuTimer.h:68
tbb
Definition: Coord.h:38
openvdb::v3_2_0::util::CpuTimer::stop
void stop() const
Print time in milliseconds since construction or start was called.
Definition: CpuTimer.h:106
openvdb::v3_2_0::util::CpuTimer::restart
void restart(const std::string &msg)
Stop previous timer, print message and re-start timer.
Definition: CpuTimer.h:92
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
openvdb::v3_2_0::util::CpuTimer::CpuTimer
CpuTimer(const std::string &msg)
Prints message and re-start timer.
Definition: CpuTimer.h:73