Created
October 21, 2024 03:45
-
-
Save TheCherno/b2c71c9291a4a1a29c889e76173c8d14 to your computer and use it in GitHub Desktop.
Timer
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> | |
class Timer | |
{ | |
public: | |
Timer() { Reset(); } | |
void Reset() { m_Start = std::chrono::high_resolution_clock::now(); } | |
float Elapsed() const { return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - m_Start).count() * 0.001f * 0.001f; } | |
float ElapsedMillis() const { return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - m_Start).count() * 0.001f; } | |
private: | |
std::chrono::time_point<std::chrono::high_resolution_clock> m_Start; | |
}; | |
class ScopedTimer | |
{ | |
public: | |
ScopedTimer(std::string_view name) : m_Name(name) {} | |
~ScopedTimer() | |
{ | |
float time = m_Timer.ElapsedMillis(); | |
std::cout << m_Name << " - " << time << "ms\n"; | |
} | |
private: | |
Timer m_Timer; | |
std::string m_Name; | |
}; |
matthewhelyar
commented
Oct 30, 2024
•
this allows you to specify the chrono units:
template<typename T>
T Elapsed() const
{
return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now() - start);
}
Recently made a similar thing in C using macro magic https://github.com/Bricktech2000/tictac.h :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment