From 341a4979322ca2832ea5685fa98616d79067c475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 21 Apr 2020 21:39:58 +0200 Subject: [PATCH] Math: benchmark vector dot() and cross(). Have to do some precision improvements, so a baseline is needed. The debug perf is beyond awful, actually. --- src/Magnum/Math/Test/CMakeLists.txt | 1 + src/Magnum/Math/Test/VectorBenchmark.cpp | 154 +++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 src/Magnum/Math/Test/VectorBenchmark.cpp diff --git a/src/Magnum/Math/Test/CMakeLists.txt b/src/Magnum/Math/Test/CMakeLists.txt index de48dbe70..930bf15e6 100644 --- a/src/Magnum/Math/Test/CMakeLists.txt +++ b/src/Magnum/Math/Test/CMakeLists.txt @@ -68,6 +68,7 @@ corrade_add_test(MathInterpolationBenchmark InterpolationBenchmark.cpp LIBRARIES corrade_add_test(MathConfigurationValueTest ConfigurationValueTest.cpp LIBRARIES MagnumMathTestLib) corrade_add_test(MathStrictWeakOrderingTest StrictWeakOrderingTest.cpp LIBRARIES MagnumMathTestLib) +corrade_add_test(MathVectorBenchmark VectorBenchmark.cpp LIBRARIES MagnumMathTestLib) corrade_add_test(MathMatrixBenchmark MatrixBenchmark.cpp LIBRARIES MagnumMathTestLib) set_property(TARGET diff --git a/src/Magnum/Math/Test/VectorBenchmark.cpp b/src/Magnum/Math/Test/VectorBenchmark.cpp new file mode 100644 index 000000000..6a690d604 --- /dev/null +++ b/src/Magnum/Math/Test/VectorBenchmark.cpp @@ -0,0 +1,154 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 + 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/Math/Vector3.h" + +namespace Magnum { namespace Math { namespace Test { namespace { + +struct VectorBenchmark: Corrade::TestSuite::Tester { + explicit VectorBenchmark(); + + void dot(); + + template void cross2Baseline(); + void cross2(); + + template void cross3Baseline(); + void cross3(); +}; + +VectorBenchmark::VectorBenchmark() { + addBenchmarks({ + &VectorBenchmark::dot, + + &VectorBenchmark::cross2Baseline, + &VectorBenchmark::cross2Baseline, + &VectorBenchmark::cross2, + + &VectorBenchmark::cross3Baseline, + &VectorBenchmark::cross3Baseline, + &VectorBenchmark::cross3, + }, 500); +} + +typedef Math::Constants Constants; +typedef Math::Vector2 Vector2; +typedef Math::Vector3 Vector3; + +enum: std::size_t { Repeats = 100000 }; + +using namespace Literals; + +void VectorBenchmark::dot() { + Vector3 a{1.3f, -1.1f, 1.0f}; + Vector3 b{4.5f, 3.2f, 7.3f}; + CORRADE_COMPARE(Math::dot(a, b), 9.63f); + + CORRADE_BENCHMARK(Repeats) { + a.x() = Math::dot(a, b); + } + + CORRADE_COMPARE(a, (Vector3{Constants::inf(), -1.1f, 1.0f})); +} + +template inline T cross2Baseline(const Vector2& v1, const Vector2& v2) { + T v1x = T(v1.x()), + v1y = T(v1.y()); + T v2x = T(v2.x()), + v2y = T(v2.y()); + return T(v1x*v2y - v1y*v2x); +} + +template void VectorBenchmark::cross2Baseline() { + setTestCaseTemplateName(TypeTraits::name()); + + Vector2 a{1.3f, -1.1f}; + Vector2 b{4.5f, 3.2f}; + CORRADE_COMPARE(Test::cross2Baseline(a, b), 9.11f); + + CORRADE_BENCHMARK(Repeats) { + a.x() = Test::cross2Baseline(a, b); + } + + CORRADE_COMPARE(a, (Vector2{Constants::inf(), -1.1f})); +} + +void VectorBenchmark::cross2() { + Vector2 a{1.3f, -1.1f}; + Vector2 b{4.5f, 3.2f}; + CORRADE_COMPARE(Math::cross(a, b), 9.11f); + + CORRADE_BENCHMARK(Repeats) { + a.x() = Math::cross(a, b); + } + + CORRADE_COMPARE(a, (Vector2{Constants::inf(), -1.1f})); +} + +template inline Vector3 cross3Baseline(const Vector3& v1, const Vector3& v2) { + T v1x = T(v1.x()), + v1y = T(v1.y()), + v1z = T(v1.z()); + T v2x = T(v2.x()), + v2y = T(v2.y()), + v2z = T(v2.z()); + return Vector3(v1y*v2z - v1z*v2y, + v1z*v2x - v1x*v2z, + v1x*v2y - v1y*v2x); +} + +template void VectorBenchmark::cross3Baseline() { + setTestCaseTemplateName(TypeTraits::name()); + + Vector3 a{1.3f, -1.1f, 1.0f}; + Vector3 b{4.5f, 3.2f, 7.3f}; + CORRADE_COMPARE(Test::cross3Baseline(a, b), + (Vector3{-11.23f, -4.99f, 9.11f})); + + CORRADE_BENCHMARK(Repeats) { + a = Test::cross3Baseline(a, b); + } + + CORRADE_VERIFY(a != a); +} + +void VectorBenchmark::cross3() { + Vector3 a{1.3f, -1.1f, 1.0f}; + Vector3 b{4.5f, 3.2f, 7.3f}; + CORRADE_COMPARE(Math::cross(a, b), + (Vector3{-11.23f, -4.99f, 9.11f})); + + CORRADE_BENCHMARK(Repeats) { + a = Math::cross(a, b); + } + + CORRADE_VERIFY(a != a); +} + +}}}} + +CORRADE_TEST_MAIN(Magnum::Math::Test::VectorBenchmark)