Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timer: Add a timer for code-benchmarking with RAII #555

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/shared/Util/CodeBench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "CodeBench.h"
#include "Log/Log.h"

ChronoTimeTracker::~ChronoTimeTracker()
{
std::chrono::nanoseconds elapsed = elapsedNanos();
sLog.outError("%s: time elapsed: %ldns, %ldµs, %ldms, %lds",
m_name.c_str(),
nanos(elapsed).count(),
micros(elapsed).count(),
millis(elapsed).count(),
secs(elapsed).count());
}
30 changes: 30 additions & 0 deletions src/shared/Util/CodeBench.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
struct ChronoTimeTracker
{
public:
ChronoTimeTracker() : m_startTime(std::chrono::steady_clock::now()), m_name("Unnamed Timer") {}
ChronoTimeTracker(std::string name) : m_startTime(std::chrono::steady_clock::now()), m_name(name) {}
~ChronoTimeTracker();
std::chrono::seconds elapsedSeconds()
{
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - m_startTime);
}
std::chrono::milliseconds elapsedMillis()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - m_startTime);
}
std::chrono::microseconds elapsedMicros()
{
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - m_startTime);
}
std::chrono::nanoseconds elapsedNanos()
{
return std::chrono::steady_clock::now() - m_startTime;
}
private:
std::chrono::time_point<std::chrono::steady_clock, std::chrono::nanoseconds> m_startTime;
std::string m_name;
inline constexpr std::chrono::nanoseconds nanos(std::chrono::nanoseconds nanos) { return nanos; }
inline constexpr std::chrono::microseconds micros(std::chrono::nanoseconds nanos) { return std::chrono::duration_cast<std::chrono::microseconds>(nanos); }
inline constexpr std::chrono::milliseconds millis(std::chrono::nanoseconds nanos) { return std::chrono::duration_cast<std::chrono::milliseconds>(nanos); }
inline constexpr std::chrono::seconds secs(std::chrono::nanoseconds nanos) { return std::chrono::duration_cast<std::chrono::seconds>(nanos); }
};