Added a profiler, which outputs time taken and calls to various functions.
[ipdf/code.git] / src / profiler.cpp
diff --git a/src/profiler.cpp b/src/profiler.cpp
new file mode 100644 (file)
index 0000000..4348603
--- /dev/null
@@ -0,0 +1,40 @@
+#include "profiler.h"
+#include "log.h"
+
+#include "SDL.h" 
+
+using namespace IPDF;
+
+Profiler IPDF::g_profiler;
+
+void Profiler::BeginZone(std::string name)
+{
+       if (!m_zones.count(name))
+               m_zones[name] = ProfileZone{0,0,0,0,0,0};
+       m_zones[name].tics_begin = SDL_GetPerformanceCounter();
+       m_zone_stack.push(name);
+}
+
+void Profiler::EndZone()
+{
+       std::string name = m_zone_stack.top();
+       m_zone_stack.pop();
+       m_zones[name].tics_end = SDL_GetPerformanceCounter();
+       m_zones[name].tics_frame += m_zones[name].tics_end - m_zones[name].tics_begin;
+       m_zones[name].tics_total += m_zones[name].tics_end - m_zones[name].tics_begin;
+       m_zones[name].calls_frame++;
+       m_zones[name].calls_total++;
+}
+
+void Profiler::EndFrame()
+{
+       // Zero all of the frame counts
+       for (auto& it : m_zones)
+       {
+#ifndef PROFILER_SILENT
+               Debug("Perf: Zone \"%s\" frame: %d ms (%d calls), total: %d ms (%d calls)", it.first.c_str(), it.second.tics_frame * 1000 / SDL_GetPerformanceFrequency(), it.second.calls_frame, it.second.tics_total * 1000 / SDL_GetPerformanceFrequency(), it.second.calls_total);
+#endif
+               it.second.tics_frame = 0;
+               it.second.calls_frame = 0;
+       }
+}

UCC git Repository :: git.ucc.asn.au