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 TF_STOPWATCH_H
25 #define 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 #include <string>
37 #include <vector>
38 
39 PXR_NAMESPACE_OPEN_SCOPE
40 
57 class TfStopwatch {
58 public:
64  TF_API TfStopwatch(const std::string& name = std::string(),
65  bool share = false);
66 
70  TF_API TfStopwatch(const TfStopwatch& other);
71 
73  TF_API virtual ~TfStopwatch();
74 
81  inline void Start() {
82  _startTick = ArchGetTickTime();
83  }
84 
95  inline void Stop() {
96  _nTicks += ArchGetTickTime() - _startTick;
97  _sampleCount++;
98  }
99 
101  void Reset() {
102  _nTicks = 0;
103  _sampleCount = 0;
104  }
105 
113  void AddFrom(const TfStopwatch& t) {
114  _nTicks += t._nTicks;
115  _sampleCount += t._sampleCount;
116  }
117 
119  const std::string& GetName() const {
120  return _name;
121  }
122 
126  TF_API static TfStopwatch GetNamedStopwatch(const std::string& name);
127 
133  int64_t GetNanoseconds() const {
134  return ArchTicksToNanoseconds(_nTicks);
135  }
136 
141  int64_t GetMicroseconds() const {
142  return GetNanoseconds() / 1000;
143  }
144 
146  int64_t GetMilliseconds() const {
147  return GetMicroseconds() / 1000;
148  }
149 
155  size_t GetSampleCount() const {
156  return _sampleCount;
157  }
158 
160  double GetSeconds() const {
161  return ArchTicksToSeconds(_nTicks);
162  }
163 
173  TF_API
174  static std::vector<std::string> GetStopwatchNames();
175 
177  bool IsShared() const {
178  return _shared;
179  }
180 
184  TF_API TfStopwatch& operator=(const TfStopwatch& other);
185 
186 private:
187  uint64_t _nTicks;
188  uint64_t _startTick;
189  size_t _sampleCount;
190  std::string _name;
191  bool _shared;
192 };
193 
200 TF_API std::ostream& operator<<(std::ostream& out, const TfStopwatch& s);
201 
202 PXR_NAMESPACE_CLOSE_SCOPE
203 
204 #endif // TF_STOPWATCH_H
ARCH_API double ArchTicksToSeconds(uint64_t nTicks)
Convert a duration measured in &quot;ticks&quot;, as returned by ArchGetTickTime(), to seconds.
TF_API TfStopwatch(const std::string &name=std::string(), bool share=false)
Constructor with optionally supplied name, which is used only by GetName().
uint64_t ArchGetTickTime()
Return the current time in system-dependent units.
Definition: timing.h:64
TF_API TfStopwatch & operator=(const TfStopwatch &other)
Assignment operator.
int64_t GetMicroseconds() const
Return the accumulated time in microseconds.
Definition: stopwatch.h:141
ARCH_API int64_t ArchTicksToNanoseconds(uint64_t nTicks)
Convert a duration measured in &quot;ticks&quot;, as returned by ArchGetTickTime(), to nanoseconds.
Low-cost, high-resolution timer datatype.
Definition: stopwatch.h:57
void Reset()
Resets the accumulated time and the sample count to zero.
Definition: stopwatch.h:101
static TF_API TfStopwatch GetNamedStopwatch(const std::string &name)
Return a copy of a particular named stopwatch.
int64_t GetNanoseconds() const
Return the accumulated time in nanoseconds.
Definition: stopwatch.h:133
virtual TF_API ~TfStopwatch()
Destroy a stopwatch.
double GetSeconds() const
Return the accumulated time in seconds as a double.
Definition: stopwatch.h:160
void Stop()
Increases the accumulated time stored in the TfStopwatch.
Definition: stopwatch.h:95
void AddFrom(const TfStopwatch &t)
Adds the accumulated time and sample count from t into the TfStopwatch.
Definition: stopwatch.h:113
static TF_API std::vector< std::string > GetStopwatchNames()
Return the names of the currently shared stopwatches.
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
const std::string & GetName() const
Return the name of the TfStopwatch.
Definition: stopwatch.h:119
void Start()
Record the current time for use by the next Stop() call.
Definition: stopwatch.h:81
int64_t GetMilliseconds() const
Return the accumulated time in milliseconds.
Definition: stopwatch.h:146
size_t GetSampleCount() const
Return the current sample count.
Definition: stopwatch.h:155
bool IsShared() const
Returns true if this stopwatch is shared.
Definition: stopwatch.h:177