-
-
Save surajRathi/7dd8f65f1ecc33e19eca5d2eb4f3ff55 to your computer and use it in GitHub Desktop.
Simple scope based code execution time printer.
This file contains 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
#include <chrono> | |
#include <iostream> | |
#include <string> | |
class TimeIt { | |
private: | |
std::string m_name; | |
std::chrono::time_point<std::chrono::high_resolution_clock> m_beg; | |
public: | |
explicit TimeIt(std::string name) : m_name(std::move(name)), m_beg(std::chrono::high_resolution_clock::now()) { | |
std::cout << m_name << ": Starting" << std::endl; | |
} | |
~TimeIt() { | |
auto end = std::chrono::high_resolution_clock::now(); | |
// Timings in release are on a different scale. | |
#ifdef NDEBUG | |
auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(end - m_beg); | |
std::cout << m_name << ": " << dur.count() << " ms" << std::endl; | |
#else | |
auto dur = std::chrono::duration_cast<std::chrono::seconds>(end - m_beg); | |
std::cout << m_name << ": " << dur.count() << " s" << std::endl; | |
#endif | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment