Skip to content

Instantly share code, notes, and snippets.

@Varun-MS
Last active May 4, 2019 14:53
Show Gist options
  • Select an option

  • Save Varun-MS/bb16f65e95e7db8b77bb5133674e1d07 to your computer and use it in GitHub Desktop.

Select an option

Save Varun-MS/bb16f65e95e7db8b77bb5133674e1d07 to your computer and use it in GitHub Desktop.
A function timer I wrote that automatically times functions, and prints the times to the console of an OpenFrameworks app. It needs to be declared on the stack to work correctly (uses the principle of object lifetimes for timing). I use it to profile the performance of my AI Algorithms.
#pragma once
#include <chrono>
#include <ofLog.h>
#include <string>
namespace AI
{
namespace PerformanceTools
{
class FunctionTimer
{
public:
FunctionTimer(const std::string& i_functionName)
{
m_functionName = i_functionName;
m_start = std::chrono::high_resolution_clock::now();
}
~FunctionTimer()
{
m_end = std::chrono::high_resolution_clock::now();
m_duration = m_end - m_start;
float durationMS = m_duration.count() * 1000.f;
ofLog() << "PERFORMANCE MEASUREMENT: Function '" << m_functionName << "' took " << durationMS << " milliseconds to execute";
}
private:
std::chrono::time_point<std::chrono::steady_clock> m_start, m_end;
std::chrono::duration<float> m_duration;
std::string m_functionName;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment