-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.hpp
37 lines (30 loc) · 875 Bytes
/
timer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once
#include <fmt/format.h>
#include <chrono>
#include <string>
#include <string_view>
#include <utility>
#include <cstdio>
struct Timer
{
std::string onScopeExit = "total";
const std::chrono::steady_clock::time_point start =
std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point timePoint = start;
auto dt(bool absolute = false)
{
auto stop = std::chrono::steady_clock::now();
return std::chrono::duration_cast<std::chrono::nanoseconds>(
stop - (absolute ? start : std::exchange(timePoint, stop)))
.count() *
1E-9;
}
void report(std::string_view description, bool absolute = false)
{
fmt::print(stderr, "time ({}) = {:.3}\n", description, dt(absolute));
}
~Timer()
{
report(onScopeExit, true);
}
};