Last active
December 4, 2022 13:48
-
-
Save rep-movsd/0ca7e38074902fa04ef96c1565ac4110 to your computer and use it in GitHub Desktop.
Benchmark code for the indexed iterator (see comments for results)
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
#include <algorithm> | |
#include <numeric> | |
#include <vector> | |
#include "benchmark/benchmark.h" | |
#include "buf_idx_iter.hpp" | |
template <typename T> | |
void BM_std_sort(benchmark::State& state) | |
{ | |
// Set up the test data | |
std::size_t size = state.range(0); | |
std::vector<T> data(size); | |
std::iota(data.begin(), data.end(), 0); | |
std::random_shuffle(data.begin(), data.end()); | |
// Measure the performance of std::sort | |
for (auto _ : state) | |
{ | |
std::sort(data.begin(), data.end()); | |
} | |
} | |
template <typename T> | |
void BM_buf_idx_iter_sort(benchmark::State& state) | |
{ | |
// Set up the test data | |
std::size_t size = state.range(0); | |
std::vector<T> data(size); | |
std::iota(data.begin(), data.end(), 0); | |
std::random_shuffle(data.begin(), data.end()); | |
// Measure the performance of std::sort on std::vector | |
for (auto _ : state) | |
{ | |
std::sort(ibegin(data), iend(data)); | |
} | |
} | |
template <typename T> | |
void BM_std_accumulate(benchmark::State& state) | |
{ | |
// Set up the test data | |
std::size_t size = state.range(0); | |
std::vector<T> data(size); | |
std::iota(data.begin(), data.end(), 0); | |
// Measure the performance of std::accumulate | |
for (auto _ : state) | |
{ | |
benchmark::DoNotOptimize(std::accumulate(data.begin(), data.end(), T{})); | |
} | |
} | |
template <typename T> | |
void BM_buf_idx_iter_accumulate(benchmark::State& state) | |
{ | |
// Set up the test data | |
std::size_t size = state.range(0); | |
std::vector<T> data(size); | |
std::iota(data.begin(), data.end(), 0); | |
// Measure the performance of std::accumulate on std::vector | |
for (auto _ : state) | |
{ | |
benchmark::DoNotOptimize(std::accumulate(ibegin(data), iend(data), T{})); | |
} | |
} | |
template <typename T> | |
void BM_std_copy_backward(benchmark::State& state) | |
{ | |
// Set up the test data | |
std::size_t size = state.range(0); | |
std::vector<T> data(size); | |
std::iota(data.begin(), data.end(), 0); | |
std::vector<T> dest(size); | |
// Measure the performance of std::copy_backward | |
for (auto _ : state) | |
{ | |
std::copy_backward(data.begin(), data.end(), dest.end()); | |
} | |
} | |
template <typename T> | |
void BM_buf_idx_iter_copy_backward(benchmark::State& state) | |
{ | |
// Set up the test data | |
std::size_t size = state.range(0); | |
std::vector<T> data(size); | |
std::iota(data.begin(), data.end(), 0); | |
std::vector<T> dest(size); | |
// Measure the performance of std::copy_backward on std::vector | |
for (auto _ : state) | |
{ | |
std::copy_backward(ibegin(data), iend(data), dest.end()); | |
} | |
} | |
template <typename T> | |
void BM_std_copy(benchmark::State& state) | |
{ | |
// Set up the test data | |
std::size_t size = state.range(0); | |
std::vector<T> data(size); | |
std::iota(data.begin(), data.end(), 0); | |
std::vector<T> dest(size); | |
// Measure the performance of std::copy | |
for (auto _ : state) | |
{ | |
std::copy(data.begin(), data.end(), dest.begin()); | |
} | |
} | |
template <typename T> | |
void BM_buf_idx_iter_copy(benchmark::State& state) | |
{ | |
// Set up the test data | |
std::size_t size = state.range(0); | |
std::vector<T> data(size); | |
std::iota(data.begin(), data.end(), 0); | |
std::vector<T> dest(size); | |
// Measure the performance of std::copy on std::vector | |
for (auto _ : state) | |
{ | |
std::copy(ibegin(data), iend(data), dest.begin()); | |
} | |
} | |
BENCHMARK_TEMPLATE(BM_std_sort, size_t)->Range(1 << 10, 1 << 24); | |
BENCHMARK_TEMPLATE(BM_buf_idx_iter_sort, size_t)->Range(1 << 10, 1 << 24); | |
BENCHMARK_TEMPLATE(BM_std_accumulate, size_t)->Range(1 << 10, 1 << 24); | |
BENCHMARK_TEMPLATE(BM_buf_idx_iter_accumulate, size_t)->Range(1 << 10, 1 << 24); | |
BENCHMARK_TEMPLATE(BM_std_copy_backward, size_t)->Range(1 << 10, 1 << 24); | |
BENCHMARK_TEMPLATE(BM_buf_idx_iter_copy_backward, size_t)->Range(1 << 10, 1 << 24); | |
BENCHMARK_TEMPLATE(BM_std_copy, size_t)->Range(1 << 10, 1 << 24); | |
BENCHMARK_TEMPLATE(BM_buf_idx_iter_copy, size_t)->Range(1 << 10, 1 << 24); | |
// remove_if doesnt work | |
//BENCHMARK_TEMPLATE(BM_std_remove_if, size_t)->Range(1 << 10, 1 << 24); | |
//BENCHMARK_TEMPLATE(BM_buf_idx_iter_remove_if, size_t)->Range(1 << 10, 1 << 24); | |
BENCHMARK_MAIN(); | |
Author
rep-movsd
commented
Dec 4, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment