2020-01-25 10:04:31 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
2020-01-25 13:13:07 +01:00
|
|
|
// This code is part of the sfizz library and is licensed under a BSD 2-clause
|
|
|
|
|
// license. You should have receive a LICENSE.md file along with the code.
|
|
|
|
|
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
|
2019-09-15 21:14:02 +02:00
|
|
|
|
2020-01-04 17:38:51 +01:00
|
|
|
#include "SIMDHelpers.h"
|
2019-09-15 21:14:02 +02:00
|
|
|
#include <benchmark/benchmark.h>
|
|
|
|
|
#include <random>
|
|
|
|
|
#include <absl/algorithm/container.h>
|
|
|
|
|
#include "absl/types/span.h"
|
|
|
|
|
|
2019-09-21 13:01:50 +02:00
|
|
|
constexpr int bigNumber { 2399132 };
|
2019-09-15 21:14:02 +02:00
|
|
|
|
|
|
|
|
class IterOffset : public benchmark::Fixture {
|
|
|
|
|
public:
|
2020-03-11 01:11:25 +01:00
|
|
|
void SetUp(const ::benchmark::State& state) {
|
2019-09-15 21:14:02 +02:00
|
|
|
std::random_device rd { };
|
2020-01-04 17:38:51 +01:00
|
|
|
std::mt19937 gen { rd() };
|
|
|
|
|
std::uniform_real_distribution<float> dist { 0.001f, 1.0f };
|
2019-09-15 21:14:02 +02:00
|
|
|
std::uniform_int_distribution<int> jumpDist { 0, 3 };
|
|
|
|
|
source = std::vector<float>(bigNumber);
|
|
|
|
|
result.resize(state.range(0));
|
|
|
|
|
absl::c_generate(source, [&]() { return dist(gen); });
|
|
|
|
|
jumps.resize(state.range(0));
|
|
|
|
|
offsets.resize(state.range(0));
|
|
|
|
|
absl::c_generate(jumps, [&]() { return jumpDist(gen); });
|
2019-09-25 23:48:52 +02:00
|
|
|
sfz::cumsum<int>(jumps, absl::MakeSpan(offsets));
|
2019-09-15 21:14:02 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-11 00:18:18 +01:00
|
|
|
void TearDown(const ::benchmark::State& /* state */) {
|
2019-09-15 21:14:02 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<float> source;
|
|
|
|
|
std::vector<float> result;
|
|
|
|
|
std::vector<int> offsets;
|
|
|
|
|
std::vector<int> jumps;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BENCHMARK_DEFINE_F(IterOffset, Pointers)(benchmark::State& state) {
|
|
|
|
|
for (auto _ : state)
|
2020-01-04 17:38:51 +01:00
|
|
|
{
|
2019-09-25 23:48:52 +02:00
|
|
|
sfz::diff<int>(offsets, absl::MakeSpan(jumps));
|
2019-09-15 21:14:02 +02:00
|
|
|
auto jump = jumps.begin();
|
|
|
|
|
auto in = source.begin();
|
|
|
|
|
auto out = result.begin();
|
|
|
|
|
while (out < result.end() && in < source.end()) {
|
|
|
|
|
*out++ = *in;
|
|
|
|
|
in += *jump++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BENCHMARK_DEFINE_F(IterOffset, Offsets)(benchmark::State& state) {
|
|
|
|
|
for (auto _ : state)
|
2020-01-04 17:38:51 +01:00
|
|
|
{
|
2019-09-15 21:14:02 +02:00
|
|
|
auto offset = offsets.begin();
|
|
|
|
|
auto out = result.begin();
|
|
|
|
|
while (offset < offsets.end())
|
|
|
|
|
*out++ = source[*offset++];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Register the function as a benchmark
|
|
|
|
|
BENCHMARK_REGISTER_F(IterOffset, Pointers)->RangeMultiplier(4)->Range((1 << 2), (1 << 12));
|
|
|
|
|
BENCHMARK_REGISTER_F(IterOffset, Offsets)->RangeMultiplier(4)->Range((1 << 2), (1 << 12));
|
2020-01-04 17:38:51 +01:00
|
|
|
BENCHMARK_MAIN();
|