SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
Utils::AverageTiming Class Reference

Singleton timing class for average measurement of different timing blocks in loops. More...

#include <AverageTiming.h>

Inherits std::map< std::string, AverageTimingBlock * >.

Public Member Functions

 AverageTiming ()
 
 ~AverageTiming ()
 

Static Public Member Functions

static void start (const std::string &name)
 start timer for a new or existing block More...
 
static void stop (const std::string &name)
 stop timer for a running block with name More...
 
static float getTime (const std::string &name)
 get time for block with name More...
 
static float getTime (const std::vector< std::string > &names)
 get time for multiple blocks with given names More...
 
static void getTimingMessage (char *m)
 get timings formatted via string More...
 
static AverageTiminginstance ()
 singleton More...
 

Private Member Functions

void doStart (const std::string &name)
 do start timer for a new or existing block More...
 
void doStop (const std::string &name)
 do stop timer for a running block with name More...
 
float doGetTime (const std::string &name)
 do get time for block with name More...
 
float doGetTime (const std::vector< std::string > &names) const
 do get time for multiple blocks with given names More...
 
void doGetTimingMessage (char *m)
 do get timings formatted via string More...
 

Private Attributes

int _averageNumValues = 200
 
int _currentPosV = 0
 
int _currentPosH = 0
 

Detailed Description

Singleton timing class for average measurement of different timing blocks in loops.

Call start("name") to define a new timing block and start timing or start timing of an existing block. Call stop("name") to finish measurement for this block. Define a hierarchy by posV and posH which is used in ui to arrange the measurements. The first found content with posV==0 is used as reference measurement for the percental value.

Definition at line 54 of file AverageTiming.h.

Constructor & Destructor Documentation

◆ AverageTiming()

Utils::AverageTiming::AverageTiming ( )

Definition at line 20 of file AverageTiming.cpp.

21 {
22 }

◆ ~AverageTiming()

Utils::AverageTiming::~AverageTiming ( )

Definition at line 24 of file AverageTiming.cpp.

25 {
26  for (auto& block : *this)
27  {
28  if (block.second)
29  delete block.second;
30  }
31 }

Member Function Documentation

◆ doGetTime() [1/2]

float Utils::AverageTiming::doGetTime ( const std::string &  name)
private

do get time for block with name

get time for block with name

Definition at line 102 of file AverageTiming.cpp.

103 {
104  if (find(name) != end())
105  {
106  return (*this)[name]->val.average();
107  }
108  else
109  Utils::log("AverageTiming: A block with name %s does not exist!", name.c_str());
110 
111  return 0.0f;
112 }
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Definition: Utils.cpp:1103

◆ doGetTime() [2/2]

float Utils::AverageTiming::doGetTime ( const std::vector< std::string > &  names) const
private

do get time for multiple blocks with given names

get time for multiple blocks with given names

Definition at line 116 of file AverageTiming.cpp.

117 {
118  AvgFloat val(_averageNumValues, 0.0f);
119  for (const std::string& n : names)
120  {
121  val.set(getTime(n));
122  }
123 
124  return val.average();
125 }
static float getTime(const std::string &name)
get time for block with name
Utils::Averaged< float > AvgFloat
Definition: Averaged.h:85

◆ doGetTimingMessage()

void Utils::AverageTiming::doGetTimingMessage ( char *  m)
private

do get timings formatted via string

Definition at line 128 of file AverageTiming.cpp.

129 {
130  // sort vertically
131  std::vector<AverageTimingBlock*> blocks;
132  for (auto& block : AverageTiming::instance())
133  {
134  blocks.push_back(block.second);
135  }
136  std::sort(blocks.begin(),
137  blocks.end(),
138  [](AverageTimingBlock* lhs, AverageTimingBlock* rhs) -> bool
139  { return lhs->posV < rhs->posV; });
140 
141  // find reference time
142  float refTime = 1.0f;
143  if (!blocks.empty())
144  {
145  refTime = (*blocks.begin())->val.average();
146  // insert number of measurement calls
147  volatile int sizeofm = sizeof(m); // workaround against a warning in the next line
148  snprintf(m + strlen(m), sizeofm, "Num. calls: %i\n", (int)(*blocks.begin())->nCalls);
149  }
150 
151  // calculate longest blockname
152  size_t maxLen = 0;
153  for (auto* block : blocks)
154  if (block->name.length() > maxLen)
155  maxLen = block->name.length();
156 
157  // insert time measurements
158  for (auto* block : blocks)
159  {
160  float val = block->val.average();
161  float valPC = Utils::clamp(val / refTime * 100.0f, 0.0f, 100.0f);
162  string name = block->name;
163 
164  name.append(maxLen - name.length(), ' ');
165 
166  stringstream ss;
167  // for (int i = 0; i < block->posH; ++i)
168  // ss << " ";
169  ss << "%s: %4.1f ms (%3d%%)\n";
170  snprintf(m + strlen(m), sizeof(m), ss.str().c_str(), name.c_str(), val, (int)valPC);
171  }
172 } //-----------------------------------------------------------------------------
static AverageTiming & instance()
singleton
Definition: AverageTiming.h:72
T clamp(T a, T min, T max)
Definition: Utils.h:253

◆ doStart()

void Utils::AverageTiming::doStart ( const std::string &  name)
private

do start timer for a new or existing block

start timer for a new or existing block

Definition at line 64 of file AverageTiming.cpp.

65 {
66  if (find(name) == end())
67  {
68  AverageTimingBlock* block = new AverageTimingBlock(
69  _averageNumValues, name, this->_currentPosV++, this->_currentPosH);
70  (*this)[name] = block;
71  }
72 
73  // if ((*this)[name]->isStarted)
74  // SL_LOG("AverageTiming: Block with name %s started twice!", name.c_str());
75 
76  (*this)[name]->timer.start();
77  (*this)[name]->isStarted = true;
78 
79  this->_currentPosH++;
80 }

◆ doStop()

void Utils::AverageTiming::doStop ( const std::string &  name)
private

do stop timer for a running block with name

stop timer for a running block with name

Definition at line 84 of file AverageTiming.cpp.

85 {
86  if (find(name) != end())
87  {
88  if (!(*this)[name]->isStarted)
89  Utils::log("AverageTiming: Block with name %s stopped without being started!", name.c_str());
90  (*this)[name]->timer.stop();
91  (*this)[name]->val.set((*this)[name]->timer.elapsedTimeInMilliSec());
92  (*this)[name]->nCalls++;
93  (*this)[name]->isStarted = false;
94  this->_currentPosH--;
95  }
96  else
97  Utils::log("AverageTiming: A block with name %s does not exist!", name.c_str());
98 }

◆ getTime() [1/2]

float Utils::AverageTiming::getTime ( const std::string &  name)
static

get time for block with name

Definition at line 46 of file AverageTiming.cpp.

47 {
48  return AverageTiming::instance().doGetTime(name);
49 }
float doGetTime(const std::string &name)
do get time for block with name

◆ getTime() [2/2]

float Utils::AverageTiming::getTime ( const std::vector< std::string > &  names)
static

get time for multiple blocks with given names

Definition at line 52 of file AverageTiming.cpp.

53 {
54  return AverageTiming::instance().doGetTime(names);
55 }

◆ getTimingMessage()

void Utils::AverageTiming::getTimingMessage ( char *  m)
static

get timings formatted via string

Definition at line 58 of file AverageTiming.cpp.

59 {
61 }
void doGetTimingMessage(char *m)
do get timings formatted via string

◆ instance()

static AverageTiming& Utils::AverageTiming::instance ( )
inlinestatic

singleton

Definition at line 72 of file AverageTiming.h.

73  {
74  static AverageTiming timing;
75  return timing;
76  }

◆ start()

void Utils::AverageTiming::start ( const std::string &  name)
static

start timer for a new or existing block

Definition at line 34 of file AverageTiming.cpp.

35 {
37 }
void doStart(const std::string &name)
do start timer for a new or existing block

◆ stop()

void Utils::AverageTiming::stop ( const std::string &  name)
static

stop timer for a running block with name

Definition at line 40 of file AverageTiming.cpp.

41 {
43 }
void doStop(const std::string &name)
do stop timer for a running block with name

Member Data Documentation

◆ _averageNumValues

int Utils::AverageTiming::_averageNumValues = 200
private

Definition at line 91 of file AverageTiming.h.

◆ _currentPosH

int Utils::AverageTiming::_currentPosH = 0
private

Definition at line 93 of file AverageTiming.h.

◆ _currentPosV

int Utils::AverageTiming::_currentPosV = 0
private

Definition at line 92 of file AverageTiming.h.


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