Created
November 9, 2019 11:56
-
-
Save Ben1980/ddc84e8fa1dcd2200447bd18a14cefc8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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