Skip to content

Instantly share code, notes, and snippets.

@ttsugriy
Created June 26, 2021 19:43
Show Gist options
  • Save ttsugriy/c39162aa6d6fd751d5084fd29d0b7fa8 to your computer and use it in GitHub Desktop.
Save ttsugriy/c39162aa6d6fd751d5084fd29d0b7fa8 to your computer and use it in GitHub Desktop.
C++ benchmark for record padding.
#include <vector>
using namespace std;
struct Record1 {
int a;
char c;
double d;
bool e;
};
struct Record2 {
double d;
int a;
char c;
bool e;
};
const int N = 10000;
vector<Record1> recs1(N);
vector<Record2> recs2(N);
template <class T>
int count_as(const vector<T> recs) {
int total = 0;
for (const auto& rec : recs) {
total += rec.a;
}
return total;
}
static void bench_1(benchmark::State& state) {
int count = 0;
for (auto _ : state) {
benchmark::DoNotOptimize(count += count_as(recs1));
}
}
BENCHMARK(bench_1);
static void bench_2(benchmark::State& state) {
int count = 0;
for (auto _ : state) {
benchmark::DoNotOptimize(count += count_as(recs2));
}
}
BENCHMARK(bench_2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment