From 09e6145990145f67676ef505635b73839a941c7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 18 Nov 2019 17:26:44 +0100 Subject: [PATCH] Math: improve Kahan Sum benchmark by comparing to doubles as well. And reducing the overhead from the vector iterators by using an Array. --- .../Math/Algorithms/Test/KahanSumTest.cpp | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/Magnum/Math/Algorithms/Test/KahanSumTest.cpp b/src/Magnum/Math/Algorithms/Test/KahanSumTest.cpp index 8efe8140c..7cacabbee 100644 --- a/src/Magnum/Math/Algorithms/Test/KahanSumTest.cpp +++ b/src/Magnum/Math/Algorithms/Test/KahanSumTest.cpp @@ -24,7 +24,7 @@ */ #include -#include +#include #include #include "Magnum/Magnum.h" @@ -39,8 +39,9 @@ struct KahanSumTest: TestSuite::Tester { void integers(); void iterative(); - void accumulate100k(); - void kahan100k(); + void accumulate100kFloats(); + void accumulate100kDoubles(); + void kahan100kFloats(); }; @@ -49,8 +50,9 @@ KahanSumTest::KahanSumTest() { &KahanSumTest::integers, &KahanSumTest::iterative}); - addBenchmarks({&KahanSumTest::accumulate100k, - &KahanSumTest::kahan100k}, 50); + addBenchmarks({&KahanSumTest::accumulate100kFloats, + &KahanSumTest::accumulate100kDoubles, + &KahanSumTest::kahan100kFloats}, 50); } /* Custom iterator class to avoid allocating half a gigabyte for hundred @@ -140,8 +142,8 @@ void KahanSumTest::iterative() { } } -void KahanSumTest::accumulate100k() { - std::vector data(100000, 1.0f); +void KahanSumTest::accumulate100kFloats() { + Containers::Array data(Containers::DirectInit, 100000, 1.0f); volatile Float a; /* to avoid optimizing the loop out */ CORRADE_BENCHMARK(10) { @@ -151,8 +153,19 @@ void KahanSumTest::accumulate100k() { CORRADE_COMPARE(Float(a), 100000.0f); } -void KahanSumTest::kahan100k() { - std::vector data(100000, 1.0f); +void KahanSumTest::accumulate100kDoubles() { + Containers::Array data(Containers::DirectInit, 100000, 1.0); + + volatile Double a; /* to avoid optimizing the loop out */ + CORRADE_BENCHMARK(10) { + a = std::accumulate(data.begin(), data.end(), 0.0); + } + + CORRADE_COMPARE(Double(a), 100000.0); +} + +void KahanSumTest::kahan100kFloats() { + Containers::Array data(Containers::DirectInit, 100000, 1.0f); volatile Float a; /* to avoid optimizing the loop out */ CORRADE_BENCHMARK(10) {