HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stopwatch.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_TF_STOPWATCH_H
25 #define PXR_BASE_TF_STOPWATCH_H
26 
27 /// \file tf/stopwatch.h
28 /// \ingroup group_tf_Performance
29 
30 #include "pxr/pxr.h"
31 
32 #include "pxr/base/arch/timing.h"
33 #include "pxr/base/tf/api.h"
34 
35 #include <iosfwd>
36 
38 
39 /// \class TfStopwatch
40 /// \ingroup group_tf_Performance
41 ///
42 /// Low-cost, high-resolution timer datatype.
43 ///
44 /// A \c TfStopwatch can be used to perform very precise timings at runtime,
45 /// even in very tight loops. The cost of "starting" or "stopping" a
46 /// \c TfStopwatch is very small: approximately 40 nanoseconds on a 900 Mhz
47 /// Pentium III Linux box, 300 nanoseconds on a 400 Mhz Sun, and 200
48 /// nanoseconds on a 250 Mhz SGI.
49 ///
50 /// Note that this class is not thread-safe: if you need to take timings in a
51 /// multi-threaded region of a process, let each thread have its own
52 /// \c TfStopwatch and then combine results using the \c AddFrom() member
53 /// function.
54 ///
56 {
57 public:
58 
59  /// Record the current time for use by the next \c Stop() call.
60  ///
61  /// The \c Start() function records the current time. A subsequent call
62  /// to \c Start() before a call to \c Stop() simply records a later
63  /// current time, but does not change the accumulated time of the \c
64  /// TfStopwatch.
65  inline void Start() {
66  _startTick = ArchGetTickTime();
67  }
68 
69  /// Increases the accumulated time stored in the \c TfStopwatch.
70  ///
71  /// The \c Stop() function increases the accumulated time by the duration
72  /// between the current time and the last time recorded by a \c Start()
73  /// call. A subsequent call to \c Stop() before another call to \c
74  /// Start() will therefore double-count time and throw off the results.
75  ///
76  /// A \c TfStopwatch also counts the number of samples it has taken. The
77  /// "sample count" is simply the number of times that \c Stop() has been
78  /// called.
79  inline void Stop() {
80  _nTicks += ArchGetTickTime() - _startTick;
81  _sampleCount++;
82  }
83 
84  /// Resets the accumulated time and the sample count to zero.
85  void Reset() {
86  _nTicks = 0;
87  _sampleCount = 0;
88  }
89 
90  /// Adds the accumulated time and sample count from \c t into the \c
91  /// TfStopwatch.
92  ///
93  /// If you have several timers taking measurements, and you wish to
94  /// combine them together, you can add one timer's results into another;
95  /// for example, \c t2.AddFrom(t1) will add \c t1 's time and sample count
96  /// into \c t2.
97  void AddFrom(const TfStopwatch& t) {
98  _nTicks += t._nTicks;
99  _sampleCount += t._sampleCount;
100  }
101 
102  /// Return the accumulated time in nanoseconds.
103  ///
104  /// Note that this number can easily overflow a 32-bit counter, so take
105  /// care to save the result in an \c int64_t, and not a regular \c int or
106  /// \c long.
107  int64_t GetNanoseconds() const {
108  return ArchTicksToNanoseconds(_nTicks);
109  }
110 
111  /// Return the accumulated time in microseconds
112  ///
113  /// Note that 45 minutes will overflow a 32-bit counter, so take care to
114  /// save the result in an \c int64_t, and not a regular \c int or \c long.
115  int64_t GetMicroseconds() const {
116  return GetNanoseconds() / 1000;
117  }
118 
119  /// Return the accumulated time in milliseconds.
120  int64_t GetMilliseconds() const {
121  return GetMicroseconds() / 1000;
122  }
123 
124  /// Return the current sample count.
125  ///
126  /// The sample count, which is simply the number of calls to \c Stop()
127  /// since creation or a call to \c Reset(), is useful for computing
128  /// average running times of a repeated task.
129  size_t GetSampleCount() const {
130  return _sampleCount;
131  }
132 
133  /// Return the accumulated time in seconds as a \c double.
134  double GetSeconds() const {
135  return ArchTicksToSeconds(_nTicks);
136  }
137 
138 private:
139  uint64_t _nTicks = 0;
140  uint64_t _startTick = 0;
141  size_t _sampleCount = 0;
142 };
143 
144 /// Output a TfStopwatch, using the format seconds.
145 ///
146 /// The elapsed time in the stopwatch is output in seconds. Note that the
147 /// timer need not be stopped.
148 ///
149 /// \ingroup group_tf_DebuggingOutput
150 TF_API std::ostream& operator<<(std::ostream& out, const TfStopwatch& s);
151 
153 
154 #endif // PXR_BASE_TF_STOPWATCH_H
GLdouble s
Definition: glew.h:1390
void Stop()
Definition: stopwatch.h:79
ARCH_API double ArchTicksToSeconds(uint64_t nTicks)
#define TF_API
Definition: api.h:40
void Start()
Definition: stopwatch.h:65
TF_API std::ostream & operator<<(std::ostream &out, const TfStopwatch &s)
uint64_t ArchGetTickTime()
Definition: timing.h:64
void Reset()
Resets the accumulated time and the sample count to zero.
Definition: stopwatch.h:85
ARCH_API int64_t ArchTicksToNanoseconds(uint64_t nTicks)
void AddFrom(const TfStopwatch &t)
Definition: stopwatch.h:97
int64_t GetNanoseconds() const
Definition: stopwatch.h:107
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
int64_t GetMicroseconds() const
Definition: stopwatch.h:115
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
size_t GetSampleCount() const
Definition: stopwatch.h:129
int64_t GetMilliseconds() const
Return the accumulated time in milliseconds.
Definition: stopwatch.h:120
double GetSeconds() const
Return the accumulated time in seconds as a double.
Definition: stopwatch.h:134
GLdouble GLdouble t
Definition: glew.h:1398