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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_TF_STOPWATCH_H
8 #define PXR_BASE_TF_STOPWATCH_H
9 
10 /// \file tf/stopwatch.h
11 /// \ingroup group_tf_Performance
12 
13 #include "pxr/pxr.h"
14 
15 #include "pxr/base/arch/timing.h"
16 #include "pxr/base/tf/api.h"
17 
18 #include <iosfwd>
19 
21 
22 /// \class TfStopwatch
23 /// \ingroup group_tf_Performance
24 ///
25 /// Low-cost, high-resolution timer datatype.
26 ///
27 /// A \c TfStopwatch can be used to perform very precise timings at runtime,
28 /// even in very tight loops. The cost of "starting" or "stopping" a
29 /// \c TfStopwatch is very small: approximately 40 nanoseconds on a 900 Mhz
30 /// Pentium III Linux box, 300 nanoseconds on a 400 Mhz Sun, and 200
31 /// nanoseconds on a 250 Mhz SGI.
32 ///
33 /// Note that this class is not thread-safe: if you need to take timings in a
34 /// multi-threaded region of a process, let each thread have its own
35 /// \c TfStopwatch and then combine results using the \c AddFrom() member
36 /// function.
37 ///
39 {
40 public:
41 
42  /// Record the current time for use by the next \c Stop() call.
43  ///
44  /// The \c Start() function records the current time. A subsequent call
45  /// to \c Start() before a call to \c Stop() simply records a later
46  /// current time, but does not change the accumulated time of the \c
47  /// TfStopwatch.
48  inline void Start() {
49  _startTick = ArchGetStartTickTime();
50  }
51 
52  /// Increases the accumulated time stored in the \c TfStopwatch.
53  ///
54  /// The \c Stop() function increases the accumulated time by the duration
55  /// between the current time and the last time recorded by a \c Start()
56  /// call. A subsequent call to \c Stop() before another call to \c
57  /// Start() will therefore double-count time and throw off the results.
58  ///
59  /// A \c TfStopwatch also counts the number of samples it has taken. The
60  /// "sample count" is simply the number of times that \c Stop() has been
61  /// called.
62  inline void Stop() {
63  _nTicks += ArchGetStopTickTime() - _startTick;
64  _sampleCount++;
65  }
66 
67  /// Resets the accumulated time and the sample count to zero.
68  void Reset() {
69  _nTicks = 0;
70  _sampleCount = 0;
71  }
72 
73  /// Adds the accumulated time and sample count from \c t into the \c
74  /// TfStopwatch.
75  ///
76  /// If you have several timers taking measurements, and you wish to
77  /// combine them together, you can add one timer's results into another;
78  /// for example, \c t2.AddFrom(t1) will add \c t1 's time and sample count
79  /// into \c t2.
80  void AddFrom(const TfStopwatch& t) {
81  _nTicks += t._nTicks;
82  _sampleCount += t._sampleCount;
83  }
84 
85  /// Return the accumulated time in nanoseconds.
86  ///
87  /// Note that this number can easily overflow a 32-bit counter, so take
88  /// care to save the result in an \c int64_t, and not a regular \c int or
89  /// \c long.
90  int64_t GetNanoseconds() const {
91  return ArchTicksToNanoseconds(_nTicks);
92  }
93 
94  /// Return the accumulated time in microseconds
95  ///
96  /// Note that 45 minutes will overflow a 32-bit counter, so take care to
97  /// save the result in an \c int64_t, and not a regular \c int or \c long.
98  int64_t GetMicroseconds() const {
99  return GetNanoseconds() / 1000;
100  }
101 
102  /// Return the accumulated time in milliseconds.
103  int64_t GetMilliseconds() const {
104  return GetMicroseconds() / 1000;
105  }
106 
107  /// Return the current sample count.
108  ///
109  /// The sample count, which is simply the number of calls to \c Stop()
110  /// since creation or a call to \c Reset(), is useful for computing
111  /// average running times of a repeated task.
112  size_t GetSampleCount() const {
113  return _sampleCount;
114  }
115 
116  /// Return the accumulated time in seconds as a \c double.
117  double GetSeconds() const {
118  return ArchTicksToSeconds(_nTicks);
119  }
120 
121 private:
122  uint64_t _nTicks = 0;
123  uint64_t _startTick = 0;
124  size_t _sampleCount = 0;
125 };
126 
127 /// Output a TfStopwatch, using the format seconds.
128 ///
129 /// The elapsed time in the stopwatch is output in seconds. Note that the
130 /// timer need not be stopped.
131 ///
132 /// \ingroup group_tf_DebuggingOutput
133 TF_API std::ostream& operator<<(std::ostream& out, const TfStopwatch& s);
134 
136 
137 #endif // PXR_BASE_TF_STOPWATCH_H
void Stop()
Definition: stopwatch.h:62
ARCH_API double ArchTicksToSeconds(uint64_t nTicks)
#define TF_API
Definition: api.h:23
void Start()
Definition: stopwatch.h:48
TF_API std::ostream & operator<<(std::ostream &out, const TfStopwatch &s)
void Reset()
Resets the accumulated time and the sample count to zero.
Definition: stopwatch.h:68
GLdouble s
Definition: glad.h:3009
ARCH_API int64_t ArchTicksToNanoseconds(uint64_t nTicks)
void AddFrom(const TfStopwatch &t)
Definition: stopwatch.h:80
int64_t GetNanoseconds() const
Definition: stopwatch.h:90
GLdouble t
Definition: glad.h:2397
uint64_t ArchGetStartTickTime()
Definition: timing.h:74
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
int64_t GetMicroseconds() const
Definition: stopwatch.h:98
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
size_t GetSampleCount() const
Definition: stopwatch.h:112
uint64_t ArchGetStopTickTime()
Definition: timing.h:116
int64_t GetMilliseconds() const
Return the accumulated time in milliseconds.
Definition: stopwatch.h:103
double GetSeconds() const
Return the accumulated time in seconds as a double.
Definition: stopwatch.h:117