TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::Stopwatch Class Reference

Accurately measure durations and framerates. More...

#include <Stopwatch.h>

Public Member Functions

 Stopwatch (const std::string &name="Stopwatch")
 
void setEnabled (bool e)
 
bool enabled () const
 
double FPS () const
 
RealTime elapsedTime () const
 
RealTime smoothElapsedTime () const
 
double smoothFPS () const
 
uint64 elapsedCycles () const
 
void tick ()
 
void tock ()
 
void reset ()
 
void after (const std::string &s="")
 

Private Member Functions

void computeOverhead ()
 

Private Attributes

std::string myName
 
bool m_enabled
 
double startTime
 
std::string prevMark
 
double prevTime
 
bool inBetween
 
uint64 cycleStart
 
RealTime timeStart
 
RealTime lastTockTime
 
RealTime lastDuration
 
int64 lastCycleCount
 
double m_fps
 
double emwaFPS
 
double m_smoothFPS
 
RealTime emwaDuration
 
int64 cycleOverhead
 

Detailed Description

Accurately measure durations and framerates.

Example 1: For profiling code in the context of a rendering loop:

    sw.tick();
    ...timed code...
    sw.tock();
    screenPrintf("%f\n", sw.smoothFPS());
  

Example 2: For profiling pieces of a sequence:

  Stopwatch sw;
  slowOperation();
  sw.after("slowOperation");
  kdTree.balance();
  sw.after("Balance tree");
 

Constructor & Destructor Documentation

G3D::Stopwatch::Stopwatch ( const std::string &  name = "Stopwatch")
18  :
19  myName(myName),
20  inBetween(false), lastTockTime(-1),
22  m_smoothFPS(0), emwaDuration(0) {
24  reset();
25 }
RealTime lastDuration
Definition: Stopwatch.h:68
double emwaFPS
Definition: Stopwatch.h:75
void computeOverhead()
Definition: Stopwatch.cpp:28
RealTime emwaDuration
Definition: Stopwatch.h:79
void reset()
Definition: Stopwatch.cpp:100
double m_smoothFPS
Definition: Stopwatch.h:76
std::string myName
Definition: Stopwatch.h:48
double m_fps
Definition: Stopwatch.h:72
RealTime lastTockTime
Definition: Stopwatch.h:66
bool inBetween
Definition: Stopwatch.h:57
int64 lastCycleCount
Definition: Stopwatch.h:69

+ Here is the call graph for this function:

Member Function Documentation

void G3D::Stopwatch::after ( const std::string &  s = "")

Call after an operation has completed, with the name of the operation, to print a debug message listing the time since the previous after() call.

Does nothing if the stopwatch is disabled.

106  {
107  RealTime now = System::time();
108  if (m_enabled) {
109  debugPrintf("%s: %10s - %8fs since %s (%fs since start)\n",
110  myName.c_str(),
111  s.c_str(),
112  now - prevTime,
113  prevMark.c_str(),
114  now - startTime);
115  }
116  prevTime = now;
117  prevMark = s;
118 }
std::string __cdecl debugPrintf(const char *fmt...) G3D_CHECK_PRINTF_ARGS
Definition: debugAssert.cpp:355
bool m_enabled
Definition: Stopwatch.h:50
std::string prevMark
Definition: Stopwatch.h:53
double RealTime
Definition: G3DGameUnits.h:27
double startTime
Definition: Stopwatch.h:52
double prevTime
Definition: Stopwatch.h:54
std::string myName
Definition: Stopwatch.h:48
static RealTime time()
Definition: System.cpp:934

+ Here is the call graph for this function:

void G3D::Stopwatch::computeOverhead ( )
private

Called from the constructor.

28  {
29  cycleOverhead = 0;
30  tick();
31  tock();
33 }
void tick()
Definition: Stopwatch.cpp:36
uint64 elapsedCycles() const
Definition: Stopwatch.h:130
void tock()
Definition: Stopwatch.cpp:49
int64 cycleOverhead
Definition: Stopwatch.h:82

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

uint64 G3D::Stopwatch::elapsedCycles ( ) const
inline

The elapsed cycle time between tick and tock. An attempt is made to factor out all tick/tock overhead, so that back-to-back calls should return zero. Unreliable on non-x86 platforms.

130  {
131  return lastCycleCount;
132  }
int64 lastCycleCount
Definition: Stopwatch.h:69

+ Here is the caller graph for this function:

RealTime G3D::Stopwatch::elapsedTime ( ) const
inline

Amount of time between the most recent tick and tock calls. 0 if tick has never been called.

108  {
109  return lastDuration;
110  }
RealTime lastDuration
Definition: Stopwatch.h:68
bool G3D::Stopwatch::enabled ( ) const
inline

A stopwatch only prints output when enabled

96  {
97  return m_enabled;
98  }
bool m_enabled
Definition: Stopwatch.h:50
double G3D::Stopwatch::FPS ( ) const
inline

Returns the number of times that tick was called per wall-clock second; e.g. frames-per-second.

102  {
103  return m_fps;
104  }
double m_fps
Definition: Stopwatch.h:72
void G3D::Stopwatch::reset ( )

Reset the start time used by after() and the emwa value.

100  {
102  prevMark = "start";
103 }
std::string prevMark
Definition: Stopwatch.h:53
double startTime
Definition: Stopwatch.h:52
double prevTime
Definition: Stopwatch.h:54
static RealTime time()
Definition: System.cpp:934

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void G3D::Stopwatch::setEnabled ( bool  e)
inline
91  {
92  m_enabled = e;
93  }
bool m_enabled
Definition: Stopwatch.h:50
RealTime G3D::Stopwatch::smoothElapsedTime ( ) const
inline

Time-smoothed value that is stable to the nearest 1%. This is useful if you are displaying elapsed time in real-time and want a stable number.

115  {
116  return emwaDuration;
117  }
RealTime emwaDuration
Definition: Stopwatch.h:79
double G3D::Stopwatch::smoothFPS ( ) const
inline

Time-smoothed value of fps that is stable to the nearest integer for fps > 10 and to the first decimal place for fps <= 10. This is useful if you are displaying the frame rate in real-time and want a stable (readable) number.

123  {
124  return m_smoothFPS;
125  }
double m_smoothFPS
Definition: Stopwatch.h:76
void G3D::Stopwatch::tick ( )

Call at the beginning of the period that you want timed.

36  {
37  // This is 'alwaysAssert' instead of 'debugAssert'
38  // since people rarely profile in debug mode.
39  alwaysAssertM(! inBetween, "Stopwatch::tick() called twice in a row.");
40  inBetween = true;
41 
42  // We read RDTSC twice here, but it is more abstract to implement this
43  // way and at least we're reading the cycle count last.
46 }
static void beginCycleCount(uint64 &cycleCount)
Definition: System.h:525
RealTime timeStart
Definition: Stopwatch.h:63
static RealTime time()
Definition: System.cpp:934
bool inBetween
Definition: Stopwatch.h:57
#define alwaysAssertM(exp, message)
Definition: debugAssert.h:165
uint64 cycleStart
Definition: Stopwatch.h:60

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void G3D::Stopwatch::tock ( )

Call at the end of the period that you want timed.

49  {
51  RealTime now = System::time();
52  lastDuration = now - timeStart;
54  // Off by more than 50%
56  } else {
57  emwaDuration = lastDuration * 0.05 + emwaDuration * 0.95;
58  }
59 
61  if (lastCycleCount < 0) {
62  lastCycleCount = 0;
63  }
64 
65  if (lastTockTime != -1.0) {
66  m_fps = 1.0 / (now - lastTockTime);
67 
68  const double blend = 0.01;
69  emwaFPS = m_fps * blend + emwaFPS * (1.0 - blend);
70 
71  double maxDiscrepancyPercentage = 0.25;
72  if (abs(emwaFPS - m_fps) > max(emwaFPS, m_fps) * maxDiscrepancyPercentage) {
73  // The difference between emwa and m_fps is way off, so
74  // update emwa directly.
75  emwaFPS = m_fps * 0.20 + emwaFPS * 0.80;
76  }
77 
78  // Update m_smoothFPS only when the value varies significantly.
79  // We round so as to not mislead the user as to the accuracy of
80  // the number.
81  if (m_smoothFPS == 0) {
83  } else if (emwaFPS <= 20) {
84  if (::fabs(m_smoothFPS - emwaFPS) > 0.75) {
85  // Small number and display is off by more than 0.75; round to the nearest 0.1
86  m_smoothFPS = floor(emwaFPS * 10.0 + 0.5) / 10.0;
87  }
88  } else if (::fabs(m_smoothFPS - emwaFPS) > 1.25) {
89  // Large number and display is off by more than 1.25; round to the nearest 1.0
90  m_smoothFPS = floor(emwaFPS + 0.5);
91  }
92  }
93  lastTockTime = now;
94 
95  alwaysAssertM(inBetween, "Stopwatch::tock() called without matching tick.");
96  inBetween = false;
97 }
RealTime lastDuration
Definition: Stopwatch.h:68
double emwaFPS
Definition: Stopwatch.h:75
RealTime timeStart
Definition: Stopwatch.h:63
double abs(double fValue)
Definition: g3dmath.h:617
static Vector3int16 floor(const Vector3 &v)
RealTime emwaDuration
Definition: Stopwatch.h:79
double RealTime
Definition: G3DGameUnits.h:27
T max(const T &x, const T &y)
Definition: g3dmath.h:320
double m_smoothFPS
Definition: Stopwatch.h:76
double m_fps
Definition: Stopwatch.h:72
RealTime lastTockTime
Definition: Stopwatch.h:66
static RealTime time()
Definition: System.cpp:934
int64 cycleOverhead
Definition: Stopwatch.h:82
bool inBetween
Definition: Stopwatch.h:57
int64 lastCycleCount
Definition: Stopwatch.h:69
#define alwaysAssertM(exp, message)
Definition: debugAssert.h:165
static void endCycleCount(uint64 &cycleCount)
Definition: System.h:530
uint64 cycleStart
Definition: Stopwatch.h:60

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

int64 G3D::Stopwatch::cycleOverhead
private

The overhead for calling into the class.

uint64 G3D::Stopwatch::cycleStart
private

The initial cycle count.

RealTime G3D::Stopwatch::emwaDuration
private

Weighted duration

double G3D::Stopwatch::emwaFPS
private

Weighted fps

bool G3D::Stopwatch::inBetween
private

True between tick and tock

int64 G3D::Stopwatch::lastCycleCount
private
RealTime G3D::Stopwatch::lastDuration
private
RealTime G3D::Stopwatch::lastTockTime
private

The time at which the previous tock was called, -1 if never.

bool G3D::Stopwatch::m_enabled
private
double G3D::Stopwatch::m_fps
private

Frames per second.

double G3D::Stopwatch::m_smoothFPS
private
std::string G3D::Stopwatch::myName
private
std::string G3D::Stopwatch::prevMark
private
double G3D::Stopwatch::prevTime
private
double G3D::Stopwatch::startTime
private
RealTime G3D::Stopwatch::timeStart
private

The time at which tick was called.


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