Skip to content

Instantly share code, notes, and snippets.

@rr-codes
Last active May 19, 2020 04:25
Show Gist options
  • Select an option

  • Save rr-codes/d4c18dacef629d068629561966cfdfa4 to your computer and use it in GitHub Desktop.

Select an option

Save rr-codes/d4c18dacef629d068629561966cfdfa4 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <benchmark/benchmark.h>
#include <random>
#include <etl/algorithm.h>
static constexpr size_t SIZE = 10000;
static constexpr size_t REPETITIONS = 10000;
static constexpr size_t RANGE_MAX = 1000;
template<size_t Size>
void setup(int (&array)[Size]) {
std::mt19937_64 engine{0};
std::uniform_int_distribution<int> distribution{0, RANGE_MAX};
for (int& i : array) {
i = distribution(engine);
}
}
static void ETL_Count(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
auto n = etl::count(etl::begin(array), etl::end(array), i % RANGE_MAX);
}
}
}
// Register the function as a benchmark
BENCHMARK(ETL_Count);
static void STL_Count(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
auto n = std::count(std::begin(array), std::end(array), i % RANGE_MAX);
}
}
}
// Register the function as a benchmark
BENCHMARK(STL_Count);
static void ETL_CountIf(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
auto n = etl::count_if(
etl::begin(array),
etl::end(array),
[](auto i) {return i % 2 == 0;}
);
}
}
}
// Register the function as a benchmark
BENCHMARK(ETL_CountIf);
static void STL_CountIf(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
auto n = std::count_if(
std::begin(array),
std::end(array),
[](auto i) {return i % 2 == 0;}
);
}
}
}
// Register the function as a benchmark
BENCHMARK(STL_CountIf);
static void ETL_Copy(benchmark::State& state) {
int array[SIZE];
int copy[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
auto n = etl::copy(etl::begin(array), etl::end(array), etl::begin(copy));
}
}
}
// Register the function as a benchmark
BENCHMARK(ETL_Copy);
static void STL_Copy(benchmark::State& state) {
int array[SIZE];
int copy[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
auto n = std::copy(std::begin(array), std::end(array), std::begin(copy));
}
}
}
// Register the function as a benchmark
BENCHMARK(STL_Copy);
static void ETL_CopyIf(benchmark::State& state) {
int array[SIZE];
int copy[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
auto n = etl::copy_if(
etl::begin(array),
etl::end(array),
etl::begin(copy),
[](auto i) { return i % 2 == 0; }
);
}
}
}
// Register the function as a benchmark
BENCHMARK(ETL_CopyIf);
static void STL_CopyIf(benchmark::State& state) {
int array[SIZE];
int copy[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
auto n = std::copy_if(
std::begin(array),
std::end(array),
std::begin(copy),
[](auto i) { return i % 2 == 0; }
);
}
}
}
// Register the function as a benchmark
BENCHMARK(STL_CopyIf);
static void ETL_Find(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
auto n = etl::find(
etl::begin(array),
etl::end(array),
i % 1000
);
}
}
}
// Register the function as a benchmark
BENCHMARK(ETL_Find);
static void STL_Find(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
auto n = std::find(
std::begin(array),
std::end(array),
i % 1000
);
}
}
}
// Register the function as a benchmark
BENCHMARK(STL_Find);
static void ETL_FindIf(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
auto n = etl::find_if(
etl::begin(array),
etl::end(array),
[](auto i) { return i % 2 == 0; }
);
}
}
}
// Register the function as a benchmark
BENCHMARK(ETL_FindIf);
static void STL_FindIf(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
auto n = std::find_if(
std::begin(array),
std::end(array),
[](auto i) { return i % 2 == 0; }
);
}
}
}
// Register the function as a benchmark
BENCHMARK(STL_FindIf);
static void ETL_Equal(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
auto n = etl::equal(etl::begin(array), etl::end(array), etl::begin(array));
}
}
}
// Register the function as a benchmark
BENCHMARK(ETL_Equal);
static void STL_Equal(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
auto n = std::equal(std::begin(array), std::end(array), std::begin(array));
}
}
}
// Register the function as a benchmark
BENCHMARK(STL_Equal);
static void ETL_Fill(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
etl::fill(etl::begin(array), etl::end(array), i % 1000);
}
}
}
// Register the function as a benchmark
BENCHMARK(ETL_Fill);
static void STL_Fill(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
std::fill(std::begin(array), std::end(array), i % 1000);
}
}
}
// Register the function as a benchmark
BENCHMARK(STL_Fill);
static void ETL_FillN(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
etl::fill_n(etl::begin(array), SIZE, i % 1000);
}
}
}
// Register the function as a benchmark
BENCHMARK(ETL_FillN);
static void STL_FillN(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
std::fill_n(std::begin(array), SIZE, i % 1000);
}
}
}
// Register the function as a benchmark
BENCHMARK(STL_FillN);
static void ETL_transform(benchmark::State& state) {
int array[SIZE];
int transformed[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
etl::transform(
etl::begin(array),
etl::end(array),
etl::begin(transformed),
[](int i) { return i + 1; }
);
}
}
}
// Register the function as a benchmark
BENCHMARK(ETL_transform);
static void STL_transform(benchmark::State& state) {
int array[SIZE];
int transformed[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
std::transform(
std::begin(array),
std::end(array),
std::begin(transformed),
[](int i) { return i + 1; }
);
}
}
}
// Register the function as a benchmark
BENCHMARK(STL_transform);
static void ETL_swap(benchmark::State& state) {
int array[SIZE];
int swapped[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
etl::swap(array, swapped);
}
}
}
// Register the function as a benchmark
BENCHMARK(ETL_swap);
static void STL_swap(benchmark::State& state) {
int array[SIZE];
int swapped[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
std::swap(array, swapped);
}
}
}
// Register the function as a benchmark
BENCHMARK(STL_swap);
static void ETL_iter_swap(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
etl::iter_swap(etl::begin(array), etl::end(array));
}
}
}
// Register the function as a benchmark
BENCHMARK(ETL_iter_swap);
static void STL_iter_swap(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
std::iter_swap(std::begin(array), std::end(array));
}
}
}
// Register the function as a benchmark
BENCHMARK(STL_iter_swap);
static void ETL_swap_ranges(benchmark::State& state) {
int array[SIZE];
int swapped[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
etl::swap_ranges(etl::begin(array), etl::end(array), etl::begin(swapped));
}
}
}
// Register the function as a benchmark
BENCHMARK(ETL_swap_ranges);
static void STL_swap_ranges(benchmark::State& state) {
int array[SIZE];
int swapped[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
std::swap_ranges(std::begin(array), std::end(array), std::begin(swapped));
}
}
}
// Register the function as a benchmark
BENCHMARK(STL_swap_ranges);
static void ETL_rotate(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
etl::rotate(etl::begin(array), etl::begin(array) + 1, etl::end(array));
}
}
}
// Register the function as a benchmark
BENCHMARK(ETL_rotate);
static void STL_rotate(benchmark::State& state) {
int array[SIZE];
setup(array);
for (auto _ : state) {
for (int i = 0; i < REPETITIONS; ++i) {
std::rotate(std::begin(array), std::begin(array) + 1, std::end(array));
}
}
}
// Register the function as a benchmark
BENCHMARK(STL_rotate);
BENCHMARK_MAIN();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment