-
Notifications
You must be signed in to change notification settings - Fork 34
/
profiler.hpp
48 lines (40 loc) · 1.13 KB
/
profiler.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
38
39
40
41
42
43
44
45
46
47
48
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "log/logger.hpp"
#include <chrono>
class TicToc {
std::string_view name_;
const kagome::log::Logger &log_;
std::chrono::time_point<std::chrono::high_resolution_clock> t_;
public:
TicToc(std::string &&) = delete;
TicToc(const std::string &) = delete;
TicToc(std::string_view name, const kagome::log::Logger &log)
: name_(name), log_(log) {
t_ = std::chrono::high_resolution_clock::now();
}
void toc() {
auto prev = t_;
t_ = std::chrono::high_resolution_clock::now();
log_->info("{} lasted for {} ms",
name_,
std::chrono::duration_cast<std::chrono::milliseconds>(t_ - prev)
.count());
}
void toc(int line) {
auto prev = t_;
t_ = std::chrono::high_resolution_clock::now();
log_->info("{} at line {} lasted for {} ms",
name_,
std::to_string(line),
std::chrono::duration_cast<std::chrono::milliseconds>(t_ - prev)
.count());
}
~TicToc() {
toc();
}
};