]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Optimize Profiler::avg()
authorgregorycu <gregory.currie@gmail.com>
Wed, 21 Jan 2015 13:25:06 +0000 (00:25 +1100)
committerShadowNinja <shadowninja@minetest.net>
Sun, 8 Mar 2015 01:04:01 +0000 (20:04 -0500)
src/profiler.h
src/test.cpp

index 5816f05ca66652960034708e4bc60c58fcdcb0ef..e5bb760c6dcbecc349dc605241f1a2a59766e0cc 100644 (file)
@@ -69,23 +69,11 @@ class Profiler
        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 @@ class Profiler
                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);
index 7b82a2c318840e00b8dfa6fb29df02d3293c4021..8c9f26c5227fd8639a05c183712a3293078c861b 100644 (file)
@@ -43,6 +43,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "util/serialize.h"
 #include "noise.h" // PseudoRandom used for random data for compression
 #include "network/networkprotocol.h" // LATEST_PROTOCOL_VERSION
+#include "profiler.h"
 #include <algorithm>
 
 /*
@@ -2118,6 +2119,40 @@ struct TestConnection: public TestBase
        }
 };
 
+struct TestProfiler : public TestBase
+{
+       void Run()
+       {
+               Profiler p;
+
+               p.avg("Test1", 1.f);
+               UASSERT(p.getValue("Test1") == 1.f);
+
+               p.avg("Test1", 2.f);
+               UASSERT(p.getValue("Test1") == 1.5f);
+
+               p.avg("Test1", 3.f);
+               UASSERT(p.getValue("Test1") == 2.f);
+
+               p.avg("Test1", 486.f);
+               UASSERT(p.getValue("Test1") == 123.f);
+
+               p.avg("Test1", 8);
+               UASSERT(p.getValue("Test1") == 100.f);
+
+               p.avg("Test1", 700);
+               UASSERT(p.getValue("Test1") == 200.f);
+
+               p.avg("Test1", 10000);
+               UASSERT(p.getValue("Test1") == 1600.f);
+
+               p.avg("Test2", 123.56);
+               p.avg("Test2", 123.58);
+
+               UASSERT(p.getValue("Test2") == 123.57f);
+       }
+};
+
 #define TEST(X) do {\
        X x;\
        infostream<<"Running " #X <<std::endl;\
@@ -2155,6 +2190,7 @@ void run_tests()
        TEST(TestCompress);
        TEST(TestSerialization);
        TEST(TestNodedefSerialization);
+       TEST(TestProfiler);
        TESTPARAMS(TestMapNode, ndef);
        TESTPARAMS(TestVoxelManipulator, ndef);
        TESTPARAMS(TestVoxelAlgorithms, ndef);