-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperf.hpp
34 lines (26 loc) · 839 Bytes
/
perf.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
#pragma once
#include <chrono>
#include <functional>
#include <iostream>
namespace dsp
{
namespace detail
{
struct defer
{
explicit defer(std::function<void()>&& fn) : fn(std::move(fn)) {}
~defer() {
fn();
}
std::function<void()> fn;
};
} /* namespace detail */
template<typename M, typename F>
auto perf(M&& msg, F&& fn) {
auto start = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
detail::defer _([start,&msg]{
std::cout << std::forward<M>(msg) << ' ' << (std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count() - start) << "µs" << std::endl;
});
return std::forward<F>(fn)();
}
} /* namespace dsp */