Skip to content

Commit 1ae7596

Browse files
committed
add access_benchmark.cpp
1 parent 69d76bf commit 1ae7596

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

benchmarks/access_benchmark.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#include "beman/inplace_vector/inplace_vector.hpp"
2+
3+
#include <array>
4+
#include <benchmark/benchmark.h>
5+
#include <cstddef>
6+
#include <iostream>
7+
#include <limits>
8+
#include <memory>
9+
#include <optional>
10+
#include <random>
11+
#include <vector>
12+
13+
static constexpr std::size_t IDX_SIZE = 4096;
14+
15+
template <typename T> class VectorTest : public benchmark::Fixture {
16+
void fill_vec(benchmark::State &state) {
17+
std::random_device dev;
18+
std::mt19937 rng(dev());
19+
std::uniform_int_distribution<std::mt19937::result_type> dist(
20+
std::numeric_limits<typename T::value_type>::min(),
21+
std::numeric_limits<typename T::value_type>::max());
22+
23+
vec.reserve(state.range(0));
24+
for (auto i = 0; i < state.range(0); ++i) {
25+
vec.push_back(dist(rng));
26+
}
27+
}
28+
29+
void fill_idx(benchmark::State &state) {
30+
std::random_device dev;
31+
std::mt19937 rng(dev());
32+
std::uniform_int_distribution<std::mt19937::result_type> dist(
33+
0, state.range(0));
34+
35+
idxs = std::make_unique<int[]>(IDX_SIZE);
36+
for (auto i = 0; i < IDX_SIZE; ++i) {
37+
auto idx = dist(rng);
38+
idxs.get()[i] = idx;
39+
}
40+
}
41+
42+
public:
43+
T vec;
44+
45+
std::unique_ptr<int[]> idxs;
46+
47+
void SetUp(::benchmark::State &state) {
48+
fill_vec(state);
49+
fill_idx(state);
50+
51+
// Warm up the cache for idxs
52+
auto sum = 0;
53+
for (auto i = 0; i < 128; ++i) {
54+
sum += idxs[i];
55+
}
56+
}
57+
58+
void TearDown(::benchmark::State &state) { vec = T(); }
59+
};
60+
61+
inline void random_access(benchmark::State &state, int *idxs, auto &vec) {
62+
while (state.KeepRunningBatch(IDX_SIZE)) {
63+
// while (state.KeepRunning()) {
64+
for (auto i = 0; i < IDX_SIZE; ++i) {
65+
auto idx = idxs[i];
66+
benchmark::DoNotOptimize(vec[idx]);
67+
vec[idx] = i;
68+
benchmark::ClobberMemory();
69+
}
70+
}
71+
}
72+
73+
#define BENCHMARK_INPLACE_VEC_RANDOM(SIZE) \
74+
BENCHMARK_TEMPLATE_DEFINE_F(VectorTest, inplace_vector_random_##SIZE, \
75+
beman::inplace_vector<int, SIZE>) \
76+
(benchmark::State & state) { random_access(state, idxs.get(), vec); } \
77+
BENCHMARK_REGISTER_F(VectorTest, inplace_vector_random_##SIZE) \
78+
->Arg(SIZE) \
79+
->Name("random inplace_vector");
80+
81+
BENCHMARK_TEMPLATE_DEFINE_F(VectorTest, vector_random,
82+
std::vector<int>)(benchmark::State &state) {
83+
random_access(state, idxs.get(), vec);
84+
}
85+
86+
#define BENCHMARK_VEC_RANDOM(SIZE) \
87+
BENCHMARK_REGISTER_F(VectorTest, vector_random) \
88+
->Arg(SIZE) \
89+
->Name("random vector");
90+
91+
#define BENCHMARK_BOTH_RANDOM(SIZE) \
92+
BENCHMARK_INPLACE_VEC_RANDOM(SIZE); \
93+
BENCHMARK_VEC_RANDOM(SIZE);
94+
95+
BENCHMARK_BOTH_RANDOM(3);
96+
BENCHMARK_BOTH_RANDOM(7);
97+
BENCHMARK_BOTH_RANDOM(15);
98+
BENCHMARK_BOTH_RANDOM(31);
99+
BENCHMARK_BOTH_RANDOM(63);
100+
BENCHMARK_BOTH_RANDOM(127);
101+
BENCHMARK_BOTH_RANDOM(255);
102+
BENCHMARK_BOTH_RANDOM(511);
103+
BENCHMARK_BOTH_RANDOM(1023);
104+
105+
BENCHMARK_BOTH_RANDOM(1024);
106+
BENCHMARK_BOTH_RANDOM(2048);
107+
BENCHMARK_BOTH_RANDOM(4096);
108+
BENCHMARK_BOTH_RANDOM(16384);
109+
BENCHMARK_BOTH_RANDOM(32768);
110+
BENCHMARK_BOTH_RANDOM(65536);
111+
BENCHMARK_BOTH_RANDOM(131072);
112+
BENCHMARK_BOTH_RANDOM(262144);
113+
114+
BENCHMARK_BOTH_RANDOM(524288);
115+
BENCHMARK_BOTH_RANDOM(1048576);
116+
117+
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)