Last active
May 4, 2019 14:53
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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