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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
#include <chrono>
#include <cstddef>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string>
#include <set>
class Dew
{
private:
int a, b, c;
public:
Dew(int _a, int _b, int _c)
: a(_a), b(_b), c(_c)
{}
bool operator<(const Dew& other) const
{
return (a < other.a) ||
(a == other.a && b < other.b) ||
(a == other.a && b == other.b && c < other.c);
}
};
constexpr int nof_operations{30};
std::size_t set_emplace()
{
std::set<Dew> set;
for (int i = 0; i < nof_operations; ++i)
for (int j = 0; j < nof_operations; ++j)
for (int k = 0; k < nof_operations; ++k)
set.emplace(i, j, k);
return set.size();
}
std::size_t set_insert()
{
std::set<Dew> set;
for (int i = 0; i < nof_operations; ++i)
for (int j = 0; j < nof_operations; ++j)
for (int k = 0; k < nof_operations; ++k)
set.insert(Dew(i, j, k));
return set.size();
}
// Overload operator<< for std::chrono::duration
template <typename Rep, typename Period>
std::ostream& operator<<(std::ostream& os, const std::chrono::duration<Rep, Period>& duration) {
os << duration.count() << " ";
if constexpr (std::is_same_v<Period, std::ratio<1>>) {
os << "seconds";
} else if constexpr (std::is_same_v<Period, std::milli>) {
os << "milliseconds";
} else if constexpr (std::is_same_v<Period, std::micro>) {
os << "microseconds";
} else if constexpr (std::is_same_v<Period, std::nano>) {
os << "nanoseconds";
} else if constexpr (std::is_same_v<Period, std::ratio<60>>) {
os << "minutes";
} else if constexpr (std::is_same_v<Period, std::ratio<3600>>) {
os << "hours";
} else {
os << "custom time unit";
}
return os;
}
void time_it(std::function<int()> set_test, std::string what = "")
{
const auto start = std::chrono::system_clock::now();
const auto the_size = set_test();
const auto stop = std::chrono::system_clock::now();
const std::chrono::duration<double, std::milli> time = stop - start;
if (!what.empty() && the_size)
std::cout << std::fixed << std::setprecision(2)
<< time << " for " << what << '\n';
}
int main()
{
time_it(set_insert, "cache warming...");
time_it(set_insert, "insert");
time_it(set_insert, "insert");
time_it(set_emplace, "emplace");
time_it(set_emplace, "emplace");
}
|