From 99fa28782f406675ea9746497695b39b50eaff80 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Sat, 16 May 2020 16:37:28 +0200 Subject: [PATCH] Add interpolators benchmark --- benchmarks/BM_interpolators.cpp | 86 +++++++++++++++++++++++++++++++++ benchmarks/CMakeLists.txt | 2 + 2 files changed, 88 insertions(+) create mode 100644 benchmarks/BM_interpolators.cpp diff --git a/benchmarks/BM_interpolators.cpp b/benchmarks/BM_interpolators.cpp new file mode 100644 index 00000000..5935a6de --- /dev/null +++ b/benchmarks/BM_interpolators.cpp @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: BSD-2-Clause + +// 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 + +#include "Interpolators.h" +#include "ScopedFTZ.h" +#include "absl/types/span.h" +#include +#include + +class Interpolators : public benchmark::Fixture { +public: + void SetUp(const ::benchmark::State& state) + { + std::random_device rd { }; + std::mt19937 gen { rd() }; + std::uniform_real_distribution dist { -1.0f, 1.0f }; + + const size_t numFramesIn = state.range(0); + inputBuffer = std::vector(numFramesIn + excessFrames); + + // any ratio will do, compute time will be proportional + static constexpr float ratio = 1.234; + + const size_t numFramesOut = static_cast(std::ceil(numFramesIn * ratio)); + input = absl::MakeSpan(inputBuffer.data(), numFramesIn); + output = std::vector(numFramesOut); + std::generate(input.begin(), input.end(), [&]() { return dist(gen); }); + } + + void TearDown(const ::benchmark::State& /* state */) + { + } + + std::vector inputBuffer; + absl::Span input; + std::vector output; + + enum { excessFrames = 8 }; +}; + +template +static void doInterpolation(absl::Span input, absl::Span output) +{ + const float kOutToIn = static_cast(input.size()) / output.size(); + + for (size_t iOut = 0; iOut < output.size(); ++iOut) { + float posIn = iOut * kOutToIn; + unsigned dec = static_cast(posIn); + float frac = posIn - dec; + output[iOut] = sfz::interpolate(&input[dec], frac); + } +} + +BENCHMARK_DEFINE_F(Interpolators, Linear)(benchmark::State& state) +{ + ScopedFTZ ftz; + + for (auto _ : state) { + doInterpolation(input, absl::MakeSpan(output)); + } +} + +BENCHMARK_DEFINE_F(Interpolators, Hermite3)(benchmark::State& state) +{ + ScopedFTZ ftz; + + for (auto _ : state) { + doInterpolation(input, absl::MakeSpan(output)); + } +} + +BENCHMARK_DEFINE_F(Interpolators, Bspline3)(benchmark::State& state) +{ + ScopedFTZ ftz; + + for (auto _ : state) { + doInterpolation(input, absl::MakeSpan(output)); + } +} + +BENCHMARK_REGISTER_F(Interpolators, Linear)->RangeMultiplier(4)->Range(1 << 2, 1 << 12); +BENCHMARK_REGISTER_F(Interpolators, Hermite3)->RangeMultiplier(4)->Range(1 << 2, 1 << 12); +BENCHMARK_REGISTER_F(Interpolators, Bspline3)->RangeMultiplier(4)->Range(1 << 2, 1 << 12); diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index 96595e47..1b2fcdbe 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -89,6 +89,8 @@ target_link_libraries(bm_readChunkFlac PRIVATE sfizz-sndfile) sfizz_add_benchmark(bm_resampleChunk BM_resampleChunk.cpp) target_link_libraries(bm_resampleChunk PRIVATE sfizz-sndfile) +sfizz_add_benchmark(bm_interpolators BM_interpolators.cpp) + sfizz_add_benchmark(bm_filterModulation BM_filterModulation.cpp ../src/sfizz/SfzFilter.cpp) target_link_libraries(bm_filterModulation PRIVATE sfizz-sndfile)