Browse Source

Math: improve Kahan Sum benchmark by comparing to doubles as well.

And reducing the overhead from the vector iterators by using an Array.
pull/403/head
Vladimír Vondruš 7 years ago
parent
commit
09e6145990
  1. 31
      src/Magnum/Math/Algorithms/Test/KahanSumTest.cpp

31
src/Magnum/Math/Algorithms/Test/KahanSumTest.cpp

@ -24,7 +24,7 @@
*/ */
#include <numeric> #include <numeric>
#include <vector> #include <Corrade/Containers/Array.h>
#include <Corrade/TestSuite/Tester.h> #include <Corrade/TestSuite/Tester.h>
#include "Magnum/Magnum.h" #include "Magnum/Magnum.h"
@ -39,8 +39,9 @@ struct KahanSumTest: TestSuite::Tester {
void integers(); void integers();
void iterative(); void iterative();
void accumulate100k(); void accumulate100kFloats();
void kahan100k(); void accumulate100kDoubles();
void kahan100kFloats();
}; };
@ -49,8 +50,9 @@ KahanSumTest::KahanSumTest() {
&KahanSumTest::integers, &KahanSumTest::integers,
&KahanSumTest::iterative}); &KahanSumTest::iterative});
addBenchmarks({&KahanSumTest::accumulate100k, addBenchmarks({&KahanSumTest::accumulate100kFloats,
&KahanSumTest::kahan100k}, 50); &KahanSumTest::accumulate100kDoubles,
&KahanSumTest::kahan100kFloats}, 50);
} }
/* Custom iterator class to avoid allocating half a gigabyte for hundred /* Custom iterator class to avoid allocating half a gigabyte for hundred
@ -140,8 +142,8 @@ void KahanSumTest::iterative() {
} }
} }
void KahanSumTest::accumulate100k() { void KahanSumTest::accumulate100kFloats() {
std::vector<Float> data(100000, 1.0f); Containers::Array<Float> data(Containers::DirectInit, 100000, 1.0f);
volatile Float a; /* to avoid optimizing the loop out */ volatile Float a; /* to avoid optimizing the loop out */
CORRADE_BENCHMARK(10) { CORRADE_BENCHMARK(10) {
@ -151,8 +153,19 @@ void KahanSumTest::accumulate100k() {
CORRADE_COMPARE(Float(a), 100000.0f); CORRADE_COMPARE(Float(a), 100000.0f);
} }
void KahanSumTest::kahan100k() { void KahanSumTest::accumulate100kDoubles() {
std::vector<Float> data(100000, 1.0f); Containers::Array<Double> 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<Float> data(Containers::DirectInit, 100000, 1.0f);
volatile Float a; /* to avoid optimizing the loop out */ volatile Float a; /* to avoid optimizing the loop out */
CORRADE_BENCHMARK(10) { CORRADE_BENCHMARK(10) {

Loading…
Cancel
Save