Skip to content

Commit 0261e0f

Browse files
committed
forgot to commit file
1 parent 05860a1 commit 0261e0f

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

benchmarks.cc

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#include <benchmark/benchmark.h>
2+
#include <random>
3+
#include <vector>
4+
#include "vapid/soa.h"
5+
6+
using Id = unsigned short;
7+
8+
std::default_random_engine gen;
9+
std::uniform_int_distribution<Id> sensor_id_gen(0, 100);
10+
std::uniform_int_distribution<Id> object_id_gen(0, 10);
11+
std::uniform_real_distribution<double> real_gen(-10.0, 10.0);
12+
13+
struct SensorData {
14+
std::array<double, 18> xyz;
15+
16+
static SensorData random() {
17+
SensorData s;
18+
for (size_t i = 0; i < s.xyz.size(); ++i) {
19+
s.xyz[i] = real_gen(gen);
20+
}
21+
return s;
22+
};
23+
};
24+
25+
template <typename... Ts>
26+
std::ostream& operator<<(std::ostream& cout, const SensorData& s) {
27+
cout << "{" << s.xyz[0] << ", " << s.xyz[1] << ", " << s.xyz[2] << "}";
28+
return cout;
29+
}
30+
31+
struct Measurement {
32+
Id sensor_id;
33+
Id object_id;
34+
double timestamp;
35+
SensorData data;
36+
37+
static Measurement random() {
38+
Measurement m;
39+
m.sensor_id = sensor_id_gen(gen);
40+
m.object_id = object_id_gen(gen);
41+
m.timestamp = real_gen(gen);
42+
m.data = SensorData::random();
43+
return m;
44+
}
45+
};
46+
47+
template <typename... Ts>
48+
std::ostream& operator<<(std::ostream& cout, const Measurement& m) {
49+
cout << "Measurement{" << m.sensor_id << ", " << m.object_id << ", " << m.timestamp << ", " << m.data << "}";
50+
return cout;
51+
}
52+
53+
struct TestCase {
54+
vapid::soa<Id, Id, double, SensorData> measurements_soa;
55+
std::vector<Measurement> measurements_vec;
56+
57+
static TestCase random() {
58+
TestCase t;
59+
for (int i = 0; i < 100000; ++i) {
60+
Measurement m = Measurement::random();
61+
t.measurements_vec.push_back(m);
62+
t.measurements_soa.insert(m.sensor_id, m.object_id, m.timestamp, m.data);
63+
}
64+
return t;
65+
}
66+
};
67+
68+
const TestCase random_data = TestCase::random();
69+
70+
static void BM_SoaSortBySensorId(benchmark::State& state) {
71+
for (auto _ : state) {
72+
state.PauseTiming();
73+
auto soa = random_data.measurements_soa;
74+
state.ResumeTiming();
75+
76+
soa.sort_by_field<0>();
77+
benchmark::DoNotOptimize(soa.get_column<0>()[0]);
78+
}
79+
}
80+
81+
static void BM_VecSortBySensorId(benchmark::State& state) {
82+
for (auto _ : state) {
83+
state.PauseTiming();
84+
auto vec = random_data.measurements_vec;
85+
state.ResumeTiming();
86+
87+
std::stable_sort(vec.begin(),
88+
vec.end(),
89+
[](auto& m1, auto& m2) {
90+
return m1.sensor_id < m2.sensor_id;
91+
});
92+
benchmark::DoNotOptimize(vec[0]);
93+
}
94+
}
95+
96+
static void BM_SoaSumTimestamps(benchmark::State& state) {
97+
const auto& soa = random_data.measurements_soa;
98+
for (auto _ : state) {
99+
double sum = 0;
100+
for (double d : soa.get_column<2>()) {
101+
sum += d;
102+
}
103+
benchmark::DoNotOptimize(sum);
104+
}
105+
}
106+
107+
static void BM_VecSumTimestamps(benchmark::State& state) {
108+
const auto& vec = random_data.measurements_vec;
109+
for (auto _ : state) {
110+
double sum = 0;
111+
for (const auto& meas : vec) {
112+
sum += meas.timestamp;
113+
}
114+
benchmark::DoNotOptimize(sum);
115+
}
116+
}
117+
118+
119+
// Register the function as a benchmark
120+
BENCHMARK(BM_SoaSortBySensorId);
121+
BENCHMARK(BM_VecSortBySensorId);
122+
BENCHMARK(BM_SoaSumTimestamps);
123+
BENCHMARK(BM_VecSumTimestamps);
124+
// Run the benchmark
125+
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)