Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const size_t SIZE = 1000000;
void fill(int *arr) {
for(size_t i = 0; i < SIZE; ++i) {
arr[i] = i;
}
}
static void Heap(benchmark::State& state) {
int * test = new int[SIZE];
fill(test);
for (auto _ : state) {
int sum = 0;
for(size_t i = 0; i < SIZE; ++i) {
sum += test[i];
}
benchmark::DoNotOptimize(sum);
}
}
BENCHMARK(Heap);
static void Stack(benchmark::State& state) {
int test[SIZE];
fill(test);
for (auto _ : state) {
int sum = 0;
for(size_t i = 0; i < SIZE; ++i) {
sum += test[i];
}
benchmark::DoNotOptimize(sum);
}
}
BENCHMARK(Stack);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.