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 
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 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
56 {
57 public:
58 
65  inline void Start() {
66  _startTick = ArchGetStartTickTime();
67  }
68 
79  inline void Stop() {
80  _nTicks += ArchGetStopTickTime() - _startTick;
81  _sampleCount++;
82  }
83 
85  void Reset() {
86  _nTicks = 0;
87  _sampleCount = 0;
88  }
89 
97  void AddFrom(const TfStopwatch& t) {
98  _nTicks += t._nTicks;
99  _sampleCount += t._sampleCount;
100  }
101 
107  int64_t GetNanoseconds() const {
108  return ArchTicksToNanoseconds(_nTicks);
109  }
110 
115  int64_t GetMicroseconds() const {
116  return GetNanoseconds() / 1000;
117  }
118 
120  int64_t GetMilliseconds() const {
121  return GetMicroseconds() / 1000;
122  }
123 
129  size_t GetSampleCount() const {
130  return _sampleCount;
131  }
132 
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 
150 TF_API std::ostream& operator<<(std::ostream& out, const TfStopwatch& s);
151 
152 PXR_NAMESPACE_CLOSE_SCOPE
153 
154 #endif // PXR_BASE_TF_STOPWATCH_H
ARCH_API double ArchTicksToSeconds(uint64_t nTicks)
Convert a duration measured in "ticks", as returned by ArchGetTickTime(), to seconds.
TF_API std::ostream & operator<<(std::ostream &out, const TfStopwatch &s)
Output a TfStopwatch, using the format seconds.
int64_t GetMicroseconds() const
Return the accumulated time in microseconds.
Definition: stopwatch.h:115
ARCH_API int64_t ArchTicksToNanoseconds(uint64_t nTicks)
Convert a duration measured in "ticks", as returned by ArchGetTickTime(), to nanoseconds.
Low-cost, high-resolution timer datatype.
Definition: stopwatch.h:55
void Reset()
Resets the accumulated time and the sample count to zero.
Definition: stopwatch.h:85
double GetSeconds() const
Return the accumulated time in seconds as a double.
Definition: stopwatch.h:134
void Stop()
Increases the accumulated time stored in the TfStopwatch.
Definition: stopwatch.h:79
void AddFrom(const TfStopwatch &t)
Adds the accumulated time and sample count from t into the TfStopwatch.
Definition: stopwatch.h:97
int64_t GetMilliseconds() const
Return the accumulated time in milliseconds.
Definition: stopwatch.h:120
size_t GetSampleCount() const
Return the current sample count.
Definition: stopwatch.h:129
uint64_t ArchGetStartTickTime()
Get a "start" tick time for measuring an interval of time, followed by a later call to ArchGetStopTic...
Definition: timing.h:85
int64_t GetNanoseconds() const
Return the accumulated time in nanoseconds.
Definition: stopwatch.h:107
uint64_t ArchGetStopTickTime()
Get a "stop" tick time for measuring an interval of time.
Definition: timing.h:126
void Start()
Record the current time for use by the next Stop() call.
Definition: stopwatch.h:65
High-resolution, low-cost timing routines.