Last active
April 15, 2022 09:20
-
-
Save bensuperpc/17a542c1d0440ec104cd8894bb4a18d5 to your computer and use it in GitHub Desktop.
google benchmark vector type
This file contains hidden or 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
#include <algorithm> | |
#include <vector> | |
#include <benchmark/benchmark.h> | |
template<class T> | |
static void vector_add(benchmark::State& state) | |
{ | |
size_t size = state.range(0); | |
std::vector<T> vec1(size); | |
std::vector<T> vec2(size); | |
std::vector<T> vec3(size); | |
std::fill(vec1.begin(), vec1.end(), static_cast<T>(12)); | |
std::fill(vec2.begin(), vec2.end(), static_cast<T>(2)); | |
std::fill(vec2.begin(), vec2.end(), static_cast<T>(0)); | |
for (auto _ : state) { | |
for (size_t i = 0; i < size; ++i) { | |
vec3[i] = vec1[i] + vec2[i]; | |
} | |
// Make sure the variable is not optimized away by compiler | |
benchmark::DoNotOptimize(vec1); | |
benchmark::DoNotOptimize(vec2); | |
benchmark::DoNotOptimize(vec3); | |
benchmark::ClobberMemory(); | |
} | |
state.SetBytesProcessed(state.iterations() * state.range(0) * sizeof(T)); | |
state.SetItemsProcessed(state.iterations() * state.range(0)); | |
} | |
/* | |
BENCHMARK_TEMPLATE(vector_add, uint8_t) | |
->Name("uint8_t") | |
->RangeMultiplier(2) | |
->Range(1, 1048576); | |
*/ | |
BENCHMARK_TEMPLATE(vector_add, uint8_t) | |
->Arg(1048576); | |
BENCHMARK_TEMPLATE(vector_add, uint_fast8_t) | |
->Arg(1048576); | |
BENCHMARK_TEMPLATE(vector_add, uint16_t) | |
->Arg(1048576); | |
BENCHMARK_TEMPLATE(vector_add, uint_fast16_t) | |
->Arg(1048576); | |
BENCHMARK_TEMPLATE(vector_add, uint32_t) | |
->Arg(1048576); | |
BENCHMARK_TEMPLATE(vector_add, uint_fast32_t) | |
->Arg(1048576); | |
BENCHMARK_TEMPLATE(vector_add, uint64_t) | |
->Arg(1048576); | |
BENCHMARK_TEMPLATE(vector_add, uint_fast64_t) | |
->Arg(1048576); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment