Skip to content

Instantly share code, notes, and snippets.

@shawnfeng0
Created May 14, 2023 22:42
Show Gist options
  • Save shawnfeng0/efcf5cdda0b48844b7abc3887f8ad79e to your computer and use it in GitHub Desktop.
Save shawnfeng0/efcf5cdda0b48844b7abc3887f8ad79e to your computer and use it in GitHub Desktop.
#include <chrono>
class Timer {
public:
Timer() { start_ = std::chrono::high_resolution_clock::now(); }
~Timer() = default;
template <typename T>
uint64_t elapsed() {
auto now = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<T>(now - start_).count();
}
uint64_t elapsed_us() { return elapsed<std::chrono::microseconds>(); }
uint64_t elapsed_ms() { return elapsed<std::chrono::milliseconds>(); }
private:
std::chrono::time_point<std::chrono::high_resolution_clock> start_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment