3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifndef PROFILER_HEADER
21 #define PROFILER_HEADER
23 #include "common_irrlicht.h"
27 #include <jmutexautolock.h>
41 void add(const std::string &name, float value)
43 JMutexAutoLock lock(m_mutex);
45 /* No average shall have been used; mark add used as -2 */
46 core::map<std::string, int>::Node *n = m_avgcounts.find(name);
48 m_avgcounts[name] = -2;
50 if(n->getValue() == -1)
52 assert(n->getValue() == -2);
56 core::map<std::string, float>::Node *n = m_data.find(name);
60 n->setValue(n->getValue() + value);
64 void avg(const std::string &name, float value)
66 JMutexAutoLock lock(m_mutex);
68 core::map<std::string, int>::Node *n = m_avgcounts.find(name);
70 m_avgcounts[name] = 1;
72 /* No add shall have been used */
73 assert(n->getValue() != -2);
74 if(n->getValue() <= 0)
77 n->setValue(n->getValue() + 1);
81 core::map<std::string, float>::Node *n = m_data.find(name);
85 n->setValue(n->getValue() + value);
91 JMutexAutoLock lock(m_mutex);
92 for(core::map<std::string, float>::Iterator
93 i = m_data.getIterator();
94 i.atEnd() == false; i++)
96 i.getNode()->setValue(0);
101 void print(std::ostream &o)
103 JMutexAutoLock lock(m_mutex);
104 for(core::map<std::string, float>::Iterator
105 i = m_data.getIterator();
106 i.atEnd() == false; i++)
108 std::string name = i.getNode()->getKey();
110 core::map<std::string, int>::Node *n = m_avgcounts.find(name);
112 if(n->getValue() >= 1)
113 avgcount = n->getValue();
117 s32 space = clampsize - name.size();
118 for(s32 j=0; j<space; j++)
120 if(j%2 == 0 && j < space - 1)
125 o<<(i.getNode()->getValue() / avgcount);
132 core::map<std::string, float> m_data;
133 core::map<std::string, int> m_avgcounts;
136 enum ScopeProfilerType{
144 ScopeProfiler(Profiler *profiler, const std::string &name,
145 enum ScopeProfilerType type = SPT_ADD):
146 m_profiler(profiler),
152 m_timer = new TimeTaker(m_name.c_str());
155 ScopeProfiler(Profiler *profiler, const char *name,
156 enum ScopeProfilerType type = SPT_ADD):
157 m_profiler(profiler),
163 m_timer = new TimeTaker(m_name.c_str());
169 float duration_ms = m_timer->stop(true);
170 float duration = duration_ms / 1000.0;
174 m_profiler->add(m_name, duration);
177 m_profiler->avg(m_name, duration);
185 Profiler *m_profiler;
188 enum ScopeProfilerType m_type;