-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmall-datastructures.cpp
105 lines (94 loc) · 3.65 KB
/
small-datastructures.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <vector>
#include <random>
#include <algorithm>
#include <unordered_set>
#include <benchmark/benchmark.h>
template <typename T>
std::vector<T> getRandomVector(int numElements) {
std::vector<T> listOfNumbers;
listOfNumbers.reserve(numElements);
std::random_device r;
std::mt19937 mersenneTwister(r());
std::uniform_int_distribution<T> d(0);
for (int i = 0; i < numElements; i++) {
listOfNumbers.emplace_back(d(mersenneTwister));
}
return listOfNumbers;
}
template <typename T>
std::set<T> getRandomSet(int numElements) {
auto listOfNumbers = getRandomVector<T>(numElements);
return std::set<T>(listOfNumbers.begin(), listOfNumbers.end());
}
template <typename T>
std::unordered_set<T> getRandomUnorderedSet(int numElements) {
auto listOfNumbers = getRandomVector<T>(numElements);
return std::unordered_set<T>(listOfNumbers.begin(), listOfNumbers.end());
}
template <typename BeginIt, typename EndIt>
std::vector<typename std::iterator_traits<BeginIt>::value_type> getShuffledData(BeginIt &&beginIt, EndIt &&endIt) {
std::vector<typename std::iterator_traits<BeginIt>::value_type> listOfCandidates(std::forward<BeginIt>(beginIt), std::forward<EndIt>(endIt));
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(listOfCandidates.begin(), listOfCandidates.end(), g);
listOfCandidates.resize(std::min(std::distance(beginIt, endIt), 50l));
return listOfCandidates;
}
using DataType = std::uint8_t;
static constexpr auto NumElements = 20;
static void VectorStructure(benchmark::State& state) {
auto listOfNumbers = getRandomVector<DataType>(NumElements);
auto listOfCandidates = getShuffledData(listOfNumbers.begin(), listOfNumbers.end());
for (auto _ : state) {
for (auto candidate : listOfCandidates) {
bool found = false;
for (auto number : listOfNumbers) {
if (number == candidate) {
found = true;
break;
}
}
benchmark::DoNotOptimize(found);
}
}
}
// Register the function as a benchmark
BENCHMARK(VectorStructure);
static void SortedVectorStructure(benchmark::State& state) {
auto listOfNumbers = getRandomVector<DataType>(NumElements);
std::sort(listOfNumbers.begin(), listOfNumbers.end());
auto listOfCandidates = getShuffledData(listOfNumbers.begin(), listOfNumbers.end());
for (auto _ : state) {
for (auto candidate : listOfCandidates) {
bool found = std::binary_search(listOfNumbers.begin(), listOfNumbers.end(), candidate);
benchmark::DoNotOptimize(found);
}
}
}
// Register the function as a benchmark
BENCHMARK(SortedVectorStructure);
static void SetStructure(benchmark::State& state) {
auto listOfNumbers = getRandomSet<DataType>(NumElements);
auto listOfCandidates = getShuffledData(listOfNumbers.begin(), listOfNumbers.end());
for (auto _ : state) {
for (auto candidate : listOfCandidates) {
auto found = listOfNumbers.count(candidate) > 0;
benchmark::DoNotOptimize(found);
}
}
}
// Register the function as a benchmark
BENCHMARK(SetStructure);
static void UnorderedSetStructure(benchmark::State& state) {
auto listOfNumbers = getRandomUnorderedSet<DataType>(NumElements);
auto listOfCandidates = getShuffledData(listOfNumbers.begin(), listOfNumbers.end());
for (auto _ : state) {
for (auto candidate : listOfCandidates) {
auto found = listOfNumbers.count(candidate) > 0;
benchmark::DoNotOptimize(found);
}
}
}
// Register the function as a benchmark
BENCHMARK(UnorderedSetStructure);
BENCHMARK_MAIN();