diff options
author | gregorycu <gregory.currie@gmail.com> | 2015-01-22 00:25:06 +1100 |
---|---|---|
committer | ShadowNinja <shadowninja@minetest.net> | 2015-03-07 20:04:01 -0500 |
commit | 267c9f4cb4616afcf07a2a33aaca43a903ac895a (patch) | |
tree | bbef409747af8e8a313b3c87813067b22a4adb1d /src/profiler.h | |
parent | d75a0a73941259ea3ebac297803838094c7d458f (diff) | |
download | minetest-267c9f4cb4616afcf07a2a33aaca43a903ac895a.tar.gz minetest-267c9f4cb4616afcf07a2a33aaca43a903ac895a.tar.bz2 minetest-267c9f4cb4616afcf07a2a33aaca43a903ac895a.zip |
Optimize Profiler::avg()
Diffstat (limited to 'src/profiler.h')
-rw-r--r-- | src/profiler.h | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/src/profiler.h b/src/profiler.h index 5816f05ca..e5bb760c6 100644 --- a/src/profiler.h +++ b/src/profiler.h @@ -69,23 +69,11 @@ public: void avg(const std::string &name, float value) { JMutexAutoLock lock(m_mutex); - { - std::map<std::string, int>::iterator n = m_avgcounts.find(name); - if(n == m_avgcounts.end()) - m_avgcounts[name] = 1; - else{ - /* No add shall have been used */ - assert(n->second != -2); - n->second = MYMAX(n->second, 0) + 1; - } - } - { - std::map<std::string, float>::iterator n = m_data.find(name); - if(n == m_data.end()) - m_data[name] = value; - else - n->second += value; - } + int &count = m_avgcounts[name]; + + assert(count != -2); + count = MYMAX(count, 0) + 1; + m_data[name] += value; } void clear() @@ -105,6 +93,21 @@ public: printPage(o, 1, 1); } + float getValue(const std::string &name) const + { + std::map<std::string, float>::const_iterator numerator = m_data.find(name); + if (numerator == m_data.end()) + return 0.f; + + std::map<std::string, int>::const_iterator denominator = m_avgcounts.find(name); + if (denominator != m_avgcounts.end()){ + if (denominator->second >= 1) + return numerator->second / denominator->second; + } + + return numerator->second; + } + void printPage(std::ostream &o, u32 page, u32 pagecount) { JMutexAutoLock lock(m_mutex); |