-
Notifications
You must be signed in to change notification settings - Fork 57
/
cpu_cycles.h
58 lines (49 loc) · 1.49 KB
/
cpu_cycles.h
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
51
52
53
54
55
56
57
58
/*
* Copyright (c) 2023 Morwenn
* SPDX-License-Identifier: MIT
*/
#include <cstdint>
#include <utility>
#include <cpp-sort/utility/adapter_storage.h>
#include <cpp-sort/utility/metrics_tools.h>
#include <cpp-sort/detail/checkers.h>
#include "rdtsc.h"
////////////////////////////////////////////////////////////
// Tag
struct cpu_cycles_tag {};
////////////////////////////////////////////////////////////
// Metric
template<typename Sorter>
struct cpu_cycles:
cppsort::utility::adapter_storage<Sorter>,
cppsort::detail::check_iterator_category<Sorter>,
cppsort::detail::check_is_always_stable<Sorter>
{
using tag_t = cpu_cycles_tag;
using metric_t = cppsort::utility::metric<unsigned long long, tag_t>;
cpu_cycles() = default;
constexpr explicit cpu_cycles(Sorter sorter):
cppsort::utility::adapter_storage<Sorter>(std::move(sorter))
{}
template<typename... Args>
auto operator()(Args&&... args) const
-> decltype(
this->get()(std::forward<Args>(args)...),
metric_t(std::declval<unsigned long long>())
)
{
auto start = ::rdtsc();
this->get()(std::forward<Args>(args)...);
auto stop = ::rdtsc();
return metric_t(stop - start);
}
};
////////////////////////////////////////////////////////////
// is_stable specialization
namespace cppsort
{
template<typename Sorter, typename... Args>
struct is_stable<cpu_cycles<Sorter>(Args...)>:
is_stable<Sorter(Args...)>
{};
}