From aaacaa6177227b96361f7b4fe55cfa4dc80a5efa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 30 Jul 2018 11:00:27 +0200 Subject: [PATCH] Animation: benchmark TrackView to verify my performance assumptions. Turns out my performance assumptions were correct! :) --- src/Magnum/Animation/Test/Benchmark.cpp | 165 +++++++++++++++++++++++ src/Magnum/Animation/Test/CMakeLists.txt | 2 + 2 files changed, 167 insertions(+) create mode 100644 src/Magnum/Animation/Test/Benchmark.cpp diff --git a/src/Magnum/Animation/Test/Benchmark.cpp b/src/Magnum/Animation/Test/Benchmark.cpp new file mode 100644 index 000000000..66e69db61 --- /dev/null +++ b/src/Magnum/Animation/Test/Benchmark.cpp @@ -0,0 +1,165 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 + Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#include + +#include "Magnum/Animation/Track.h" + +namespace Magnum { namespace Animation { namespace Test { + +struct Benchmark: TestSuite::Tester { + explicit Benchmark(); + + void interpolateEmpty(); + void interpolateInterleaved(); + void interpolateInterleavedStrict(); + + void atEmpty(); + void at(); + void atHint(); + void atStrict(); + void atStrictInterleaved(); + void atStrictInterleavedDirectInterpolator(); + + Containers::Array _keys; + Containers::Array _values; + Containers::Array> _interleaved; + Containers::StridedArrayView _keysInterleaved; + Containers::StridedArrayView _valuesInterleaved; + TrackView _track; + TrackView _trackInterleaved; +}; + +namespace { + enum: std::size_t { DataSize = 2000 }; +} + +Benchmark::Benchmark() { + addBenchmarks({&Benchmark::interpolateEmpty, + &Benchmark::interpolateInterleaved, + &Benchmark::interpolateInterleavedStrict, + + &Benchmark::atEmpty, + &Benchmark::at, + &Benchmark::atHint, + &Benchmark::atStrict, + &Benchmark::atStrictInterleaved, + &Benchmark::atStrictInterleavedDirectInterpolator}, 10); + + _keys = Containers::Array{DataSize}; + _values = Containers::Array{Containers::DirectInit, DataSize, 1}; + _interleaved = Containers::Array>{Containers::DirectInit, DataSize, 0.0f, 1}; + for(std::size_t i = 0; i != DataSize; ++i) + _keys[i] = _interleaved[i].first = Float(i)*3.1254f; + + _keysInterleaved = {&_interleaved[0].first, _interleaved.size(), sizeof(std::pair)}; + _valuesInterleaved = {&_interleaved[0].second, _interleaved.size(), sizeof(std::pair)}; + + _track = TrackView{ + Containers::arrayView(_keys), Containers::arrayView(_values), Math::select}; + _trackInterleaved = {_keysInterleaved, _valuesInterleaved, Math::select}; +} + +void Benchmark::interpolateEmpty() { + Int result{}; + CORRADE_BENCHMARK(250) { + std::size_t hint{}; + for(Float i = 0.0f; i < 500.0f; i += 1.0f) + result += interpolate(nullptr, nullptr, {}, {}, Math::select, i, hint); + } + CORRADE_COMPARE(result, 0); +} + +void Benchmark::interpolateInterleaved() { + Int result{}; + CORRADE_BENCHMARK(250) { + std::size_t hint{}; + for(Float i = 0.0f; i < 500.0f; i += 1.0f) + result += interpolate(_keysInterleaved, _valuesInterleaved, {}, {}, Math::select, i, hint); + } + CORRADE_COMPARE(result, 125000); +} + +void Benchmark::interpolateInterleavedStrict() { + Int result{}; + CORRADE_BENCHMARK(250) { + std::size_t hint{}; + for(Float i = 0.0f; i < 500.0f; i += 1.0f) + result += interpolateStrict(_keysInterleaved, _valuesInterleaved, Math::select, i, hint); + } + CORRADE_COMPARE(result, 125000); +} + +void Benchmark::atEmpty() { + TrackView empty{nullptr, nullptr, Math::select}; + + Int result{}; + CORRADE_BENCHMARK(250) + for(Float i = 0.0f; i < 500.0f; i += 1.0f) + result += empty.at(i); + CORRADE_COMPARE(result, 0); +} + +void Benchmark::at() { + Int result{}; + CORRADE_BENCHMARK(250) + for(Float i = 0.0f; i < 500.0f; i += 1.0f) + result += _track.at(i); + CORRADE_COMPARE(result, 125000); +} + +void Benchmark::atHint() { + Int result{}; + CORRADE_BENCHMARK(250) { + std::size_t hint{}; + for(Float i = 0.0f; i < 500.0f; i += 1.0f) + result += _track.at(i, hint); + } + CORRADE_COMPARE(result, 125000); +} + +void Benchmark::atStrict() { + Int result{}; + CORRADE_BENCHMARK(250) { + std::size_t hint{}; + for(Float i = 0.0f; i < 500.0f; i += 1.0f) + result += _track.atStrict(i, hint); + } + CORRADE_COMPARE(result, 125000); +} + +void Benchmark::atStrictInterleaved() { + Int result{}; + CORRADE_BENCHMARK(250) { + std::size_t hint{}; + for(Float i = 0.0f; i < 500.0f; i += 1.0f) + result += _trackInterleaved.atStrict(i, hint); + } + CORRADE_COMPARE(result, 125000); +} + +}}} + +CORRADE_TEST_MAIN(Magnum::Animation::Test::Benchmark) diff --git a/src/Magnum/Animation/Test/CMakeLists.txt b/src/Magnum/Animation/Test/CMakeLists.txt index 636338ab2..8813397d0 100644 --- a/src/Magnum/Animation/Test/CMakeLists.txt +++ b/src/Magnum/Animation/Test/CMakeLists.txt @@ -23,6 +23,7 @@ # DEALINGS IN THE SOFTWARE. # +corrade_add_test(AnimationBenchmark Benchmark.cpp LIBRARIES Magnum) corrade_add_test(AnimationInterpolationTest InterpolationTest.cpp LIBRARIES MagnumTestLib) corrade_add_test(AnimationTrackTest TrackTest.cpp LIBRARIES Magnum) corrade_add_test(AnimationTrackViewTest TrackViewTest.cpp LIBRARIES Magnum) @@ -32,6 +33,7 @@ set_property(TARGET APPEND PROPERTY COMPILE_DEFINITIONS "CORRADE_GRACEFUL_ASSERT") set_target_properties( + AnimationBenchmark AnimationInterpolationTest AnimationTrackTest AnimationTrackViewTest