Skip to content

Instantly share code, notes, and snippets.

@jonnyyu
Created October 11, 2022 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonnyyu/eb9f9ef3cf9f2263d2bc0cfba6ceacd2 to your computer and use it in GitHub Desktop.
Save jonnyyu/eb9f9ef3cf9f2263d2bc0cfba6ceacd2 to your computer and use it in GitHub Desktop.
std::chrono based PerfCounter
#pragma once
#include <string>
#include <chrono>
namespace chrono = std::chrono;
using hrclock = chrono::high_resolution_clock;
namespace utils
{
class PerfCounter
{
string mName;
chrono::steady_clock::time_point mStart;
bool mShowDuration;
public:
PerfCounter(const string& name, bool autoStart = true, bool showDuration = true)
{
if (autoStart) {
mStart = hrclock::now();
}
}
~PerfCounter()
{
if (mShowDuration) {
log("[{}]: took {} ms\n", mName, ticks());
}
}
int64_t ticks() const
{
return ((hrclock::now() - mStart) / 1000000).count();
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment