Created
October 11, 2022 03:32
std::chrono based PerfCounter
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 <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