Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
PerfTimer.cpp
1 /*
2  Harmonic Flow Framework
3  Copyright (C) 2011-2018 Andreas Widen <andreas@harmonicflow.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "PerfTimer.h"
22 #include <assert.h>
23 
24 using namespace HFCore;
25 
26 bool PerfTimer::gIsEnabled = false;
27 PerfTimer::EntriesMap *PerfTimer::gEntries = NULL;
28 
29 #include "debug/CrtDbgNew.h"
30 
31 void PerfTimer::setEnabled(bool enabled)
32 {
33  gIsEnabled = enabled;
34  if (enabled)
35  {
36  gEntries = new EntriesMap;
37  gEntries->clear();
38  }
39  else
40  {
41  // release all PerfTimer::Entry *:
42  for (EntriesMap::iterator iter = gEntries->begin();
43  iter != gEntries->end(); iter++)
44  {
45  HF_SAFE_DELETE(iter->second);
46  }
47  HF_SAFE_DELETE(gEntries);
48  }
49 }
50 
52 {
53  return gIsEnabled;
54 }
55 
56 void PerfTimer::begin(const std::string &id)
57 {
58  if (!gIsEnabled)
59  {
60  return;
61  }
62 
63  EntriesMap::iterator iter = gEntries->find(id);
64 
65  Entry *entry;
66 
67  // if there is no entry for this id:
68  if (iter == gEntries->end())
69  {
70  // create one:
71  entry = new Entry;
72  entry->id = id;
73  entry->totalTime = 0;
74  entry->calls = 0;
75  entry->minTime = 0;
76  entry->maxTime = 0;
77  (*gEntries)[id] = entry;
78  }
79  else
80  {
81  entry = iter->second;
82  assert(entry->startTime < 0);
83  }
84 
85  // remember start time:
86  entry->startTime = clock();
87  entry->calls++;
88 }
89 
90 void PerfTimer::end(const std::string &id)
91 {
92  if (!gIsEnabled)
93  {
94  return;
95  }
96 
97  EntriesMap::iterator iter = gEntries->find(id);
98 
99  // if there is an entry for this id:
100  // (it's possible that the timer was activated between a begin/end pair
101  // and so no entry exists for a particular id. in this case we should
102  // do nothing)
103  if (iter != gEntries->end())
104  {
105  // get the entry for this id:
106  Entry *entry = iter->second;
107  assert(entry->startTime >= 0);
108 
109  // add time since begin call;
110  clock_t dt = clock() - entry->startTime;
111  entry->totalTime += dt;
112  if (entry->minTime == 0 || entry->minTime > dt)
113  {
114  entry->minTime = dt;
115  }
116  if (entry->maxTime == 0 || entry->maxTime < dt)
117  {
118  entry->maxTime = dt;
119  }
120 
121  // stop timing for this entry:
122  entry->startTime = -1;
123  }
124 }
125 
127 {
128  if (!gIsEnabled)
129  {
130  return 0;
131  }
132 
133  return (int32_hf)gEntries->size();
134 }
135 
137 {
138  if (!gIsEnabled)
139  {
140  return NULL;
141  }
142 
143  // TODO: implement this in a nicer way... this is ugly:
144  assert(index < (int32_hf)gEntries->size());
145  EntriesMap::iterator iter = gEntries->begin();
146  for (int32_hf i = 0; i < index; i++)
147  {
148  iter++;
149  }
150  return iter->second;
151 }
152 
153 const PerfTimer::Entry *PerfTimer::getEntry(const std::string &id)
154 {
155  if (!gIsEnabled)
156  {
157  return NULL;
158  }
159 
160  EntriesMap::iterator iter = gEntries->find(id);
161  if (iter == gEntries->end())
162  {
163  return NULL;
164  }
165  else
166  {
167  return iter->second;
168  }
169 }
Struct for holding timing related variables.
Definition: PerfTimer.h:76
#define HF_SAFE_DELETE(p)
Check if not NULL, delete and set to NULL.
Definition: HFDataTypes.h:49
int int32_hf
A type definition for int.
Definition: HFDataTypes.h:349
clock_t minTime
minimum time
Definition: PerfTimer.h:81
clock_t startTime
start time
Definition: PerfTimer.h:79
static void end(const std::string &id)
End PerfTiming for id.
Definition: PerfTimer.cpp:90
uint32_hf calls
total calls
Definition: PerfTimer.h:83
#define NULL
Convenient define for 0.
Definition: HFDataTypes.h:42
static const Entry * getEntry(int32_hf index)
Get PerfTimer::Entry for index.
Definition: PerfTimer.cpp:136
static void begin(const std::string &id)
Start PerfTiming for id.
Definition: PerfTimer.cpp:56
std::string id
id of this Entry. An arbitary string.
Definition: PerfTimer.h:78
static bool isEnabled()
Determines whether PerfTiming currently is enabled.
Definition: PerfTimer.cpp:51
Interface useful for performance timing.
Definition: Actor.h:34
static void setEnabled(bool enabled)
Set PerfTiming enabled / disabled state.
Definition: PerfTimer.cpp:31
std::map< std::string, Entry * > EntriesMap
typedef for map holding std::string and Entry pointer pairs.
Definition: PerfTimer.h:90
clock_t maxTime
maximum time
Definition: PerfTimer.h:82
static int32_hf getEntryCount()
Get number of PerfTimer::Entry&#39;s.
Definition: PerfTimer.cpp:126
clock_t totalTime
total time
Definition: PerfTimer.h:80