-
Notifications
You must be signed in to change notification settings - Fork 0
/
timekeeping.hpp
50 lines (43 loc) · 1.51 KB
/
timekeeping.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
49
50
// timekeeping.hpp
//
// Defines method for measuring time.
//
#pragma once
#include <chrono>
#include <tuple>
#include "acore.hpp"
namespace ALisp {
namespace Timekeeping {
typedef typename std::chrono::high_resolution_clock ClockType;
typedef typename std::chrono::milliseconds ResolutionType;
typedef long TimeType;
template<typename T = ResolutionType> StringType time_type() { return "unknown"; }
template<> StringType time_type<std::chrono::microseconds>() { return "us"; }
template<> StringType time_type<std::chrono::milliseconds>() { return "ms"; }
template<> StringType time_type<std::chrono::nanoseconds>() { return "ns"; }
template<> StringType time_type<std::chrono::seconds>() { return "s"; }
template<
typename Result,
class Callback,
typename Resolution = ResolutionType,
typename Time = TimeType>
std::tuple<Time, Result> timeCallbackResult(Callback callback) {
auto start = ClockType::now();
Result result = callback();
auto finish = ClockType::now();
auto duration = std::chrono::duration_cast<Resolution>(finish - start);
return std::make_tuple((Time)duration.count(), result);
}
template<
class Callback,
typename Resolution = ResolutionType,
typename Time = TimeType>
Time timeCall(Callback callback) {
auto start = ClockType::now();
callback();
auto finish = ClockType::now();
auto duration = std::chrono::duration_cast<Resolution>(finish - start);
return (Time)duration.count();
}
}
}