All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TfStopwatch Class Reference

Low-cost, high-resolution timer datatype. More...

Public Member Functions

void Start ()
 Record the current time for use by the next Stop() call. More...
 
void Stop ()
 Increases the accumulated time stored in the TfStopwatch. More...
 
void Reset ()
 Resets the accumulated time and the sample count to zero. More...
 
void AddFrom (const TfStopwatch &t)
 Adds the accumulated time and sample count from t into the TfStopwatch. More...
 
int64_t GetNanoseconds () const
 Return the accumulated time in nanoseconds. More...
 
int64_t GetMicroseconds () const
 Return the accumulated time in microseconds. More...
 
int64_t GetMilliseconds () const
 Return the accumulated time in milliseconds. More...
 
size_t GetSampleCount () const
 Return the current sample count. More...
 
double GetSeconds () const
 Return the accumulated time in seconds as a double. More...
 

Detailed Description

Low-cost, high-resolution timer datatype.

A TfStopwatch can be used to perform very precise timings at runtime, even in very tight loops. The cost of "starting" or "stopping" a TfStopwatch is very small: approximately 40 nanoseconds on a 900 Mhz Pentium III Linux box, 300 nanoseconds on a 400 Mhz Sun, and 200 nanoseconds on a 250 Mhz SGI.

Note that this class is not thread-safe: if you need to take timings in a multi-threaded region of a process, let each thread have its own TfStopwatch and then combine results using the AddFrom() member function.

Definition at line 55 of file stopwatch.h.

Member Function Documentation

void AddFrom ( const TfStopwatch t)
inline

Adds the accumulated time and sample count from t into the TfStopwatch.

If you have several timers taking measurements, and you wish to combine them together, you can add one timer's results into another; for example, t2.AddFrom(t1) will add t1 's time and sample count into t2.

Definition at line 97 of file stopwatch.h.

int64_t GetMicroseconds ( ) const
inline

Return the accumulated time in microseconds.

Note that 45 minutes will overflow a 32-bit counter, so take care to save the result in an int64_t, and not a regular int or long.

Definition at line 115 of file stopwatch.h.

int64_t GetMilliseconds ( ) const
inline

Return the accumulated time in milliseconds.

Definition at line 120 of file stopwatch.h.

int64_t GetNanoseconds ( ) const
inline

Return the accumulated time in nanoseconds.

Note that this number can easily overflow a 32-bit counter, so take care to save the result in an int64_t, and not a regular int or long.

Definition at line 107 of file stopwatch.h.

size_t GetSampleCount ( ) const
inline

Return the current sample count.

The sample count, which is simply the number of calls to Stop() since creation or a call to Reset(), is useful for computing average running times of a repeated task.

Definition at line 129 of file stopwatch.h.

double GetSeconds ( ) const
inline

Return the accumulated time in seconds as a double.

Definition at line 134 of file stopwatch.h.

void Reset ( )
inline

Resets the accumulated time and the sample count to zero.

Definition at line 85 of file stopwatch.h.

void Start ( )
inline

Record the current time for use by the next Stop() call.

The Start() function records the current time. A subsequent call to Start() before a call to Stop() simply records a later current time, but does not change the accumulated time of the TfStopwatch.

Definition at line 65 of file stopwatch.h.

void Stop ( )
inline

Increases the accumulated time stored in the TfStopwatch.

The Stop() function increases the accumulated time by the duration between the current time and the last time recorded by a Start() call. A subsequent call to Stop() before another call to Start() will therefore double-count time and throw off the results.

A TfStopwatch also counts the number of samples it has taken. The "sample count" is simply the number of times that Stop() has been called.

Definition at line 79 of file stopwatch.h.


The documentation for this class was generated from the following file: