diff --git a/build/CMakeLists.txt b/build/CMakeLists.txt index dcebf65a..45db2eb1 100644 --- a/build/CMakeLists.txt +++ b/build/CMakeLists.txt @@ -91,7 +91,6 @@ if(CMAKE_BUILD_TYPE MATCHES coverage) add_definitions(-O0 -g) set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} --coverage) elseif(CMAKE_BUILD_TYPE MATCHES release) - set(BUILD_CHECKS ON) if(MSVC) add_definitions(/DBOOST_DISABLE_ASSERTS) else() @@ -104,11 +103,13 @@ add_custom_target(sync_examples ALL COMMAND python ${PROJECT_SOURCE_DIR}/../doc/ # checks if(BUILD_CHECKS) + add_executable(axis_size ../test/axis_size.cpp) + # find_package(benchmark) add_executable(speed_cpp ../test/speed_cpp.cpp) + # target_link_libraries(speed_cpp -lbenchmark -lpthread) if (BUILD_PYTHON) configure_file(../test/speed_numpy.py speed_numpy.py) endif() - add_executable(axis_size ../test/axis_size.cpp) endif() # tests diff --git a/test/speed_cpp.cpp b/test/speed_cpp.cpp index 052fd5b5..7b347816 100644 --- a/test/speed_cpp.cpp +++ b/test/speed_cpp.cpp @@ -10,31 +10,49 @@ #include #include #include -#include +#include using namespace boost::histogram; namespace mpl = boost::mpl; -std::vector random_array(unsigned n, int type) { - std::vector result(n); +std::unique_ptr random_array(unsigned n, int type) { + std::unique_ptr r(new double[n]); std::default_random_engine gen(1); if (type) { // type == 1 std::normal_distribution<> d(0.5, 0.3); - for (auto &x : result) - x = d(gen); + for (unsigned i = 0; i < n; ++i) + r[i] = d(gen); } else { // type == 0 std::uniform_real_distribution<> d(0.0, 1.0); - for (auto &x : result) - x = d(gen); + for (unsigned i = 0; i < n; ++i) + r[i] = d(gen); } - return result; + return r; +} + +template void ignore( const T& ) { } + +double baseline(unsigned n) { + auto r = random_array(n, 0); + + auto best = std::numeric_limits::max(); + for (unsigned k = 0; k < 20; ++k) { + auto t = clock(); + for (unsigned i = 0; i < n; ++i) { + volatile auto x = r[i]; + ignore(x); + } + t = clock() - t; + best = std::min(best, double(t) / CLOCKS_PER_SEC); + } + return best; } template double compare_1d(unsigned n, int distrib) { auto r = random_array(n, distrib); auto best = std::numeric_limits::max(); - for (unsigned k = 0; k < 50; ++k) { + for (unsigned k = 0; k < 20; ++k) { auto h = Histogram(axis::regular<>(100, 0, 1)); auto t = clock(); for (unsigned i = 0; i < n; ++i) @@ -50,7 +68,7 @@ template double compare_2d(unsigned n, int distrib) { auto r = random_array(n, distrib); auto best = std::numeric_limits::max(); - for (unsigned k = 0; k < 50; ++k) { + for (unsigned k = 0; k < 20; ++k) { auto h = Histogram(axis::regular<>(100, 0, 1), axis::regular<>(100, 0, 1)); auto t = clock(); for (unsigned i = 0; i < n/2; ++i) @@ -66,7 +84,7 @@ template double compare_3d(unsigned n, int distrib) { auto r = random_array(n, distrib); auto best = std::numeric_limits::max(); - for (unsigned k = 0; k < 50; ++k) { + for (unsigned k = 0; k < 20; ++k) { auto h = Histogram(axis::regular<>(100, 0, 1), axis::regular<>(100, 0, 1), axis::regular<>(100, 0, 1)); auto t = clock(); @@ -83,7 +101,7 @@ template double compare_6d(unsigned n, int distrib) { auto r = random_array(n, distrib); auto best = std::numeric_limits::max(); - for (unsigned k = 0; k < 50; ++k) { + for (unsigned k = 0; k < 20; ++k) { auto h = Histogram(axis::regular<>(10, 0, 1), axis::regular<>(10, 0, 1), axis::regular<>(10, 0, 1), axis::regular<>(10, 0, 1), axis::regular<>(10, 0, 1), axis::regular<>(10, 0, 1)); @@ -101,6 +119,10 @@ template double compare_6d(unsigned n, int distrib) { } int main() { + const unsigned nfill = 6000000; + + printf("baseline %.3f\n", baseline(nfill)); + printf("1D\n"); for (int itype = 0; itype < 2; ++itype) { if (itype == 0) @@ -110,17 +132,17 @@ int main() { printf("hs_ss %.3f\n", compare_1d>, array_storage>>( - 6000000, itype)); + nfill, itype)); printf("hs_sd %.3f\n", compare_1d>, - adaptive_storage>>(6000000, itype)); + adaptive_storage>>(nfill, itype)); printf("hd_ss %.3f\n", compare_1d>>( - 6000000, itype)); + nfill, itype)); printf("hd_sd %.3f\n", compare_1d>( - 6000000, itype)); + nfill, itype)); } printf("2D\n"); @@ -132,18 +154,18 @@ int main() { printf("hs_ss %.3f\n", compare_2d, axis::regular<>>, - array_storage>>(6000000, itype)); + array_storage>>(nfill, itype)); printf("hs_sd %.3f\n", compare_2d, axis::regular<>>, - adaptive_storage>>(6000000, itype)); + adaptive_storage>>(nfill, itype)); printf("hd_ss %.3f\n", compare_2d>>( - 6000000, itype)); + nfill, itype)); printf("hd_sd %.3f\n", compare_2d>( - 6000000, itype)); + nfill, itype)); } printf("3D\n"); @@ -155,18 +177,18 @@ int main() { printf("hs_ss %.3f\n", compare_3d, axis::regular<>, axis::regular<>>, - array_storage>>(6000000, itype)); + array_storage>>(nfill, itype)); printf("hs_sd %.3f\n", compare_3d, axis::regular<>, axis::regular<>>, - adaptive_storage>>(6000000, itype)); + adaptive_storage>>(nfill, itype)); printf("hd_ss %.3f\n", compare_3d>>( - 6000000, itype)); + nfill, itype)); printf("hd_sd %.3f\n", compare_3d>( - 6000000, itype)); + nfill, itype)); } printf("6D\n"); @@ -179,18 +201,18 @@ int main() { compare_6d, axis::regular<>, axis::regular<>, axis::regular<>, axis::regular<>, axis::regular<>>, - array_storage>>(6000000, itype)); + array_storage>>(nfill, itype)); printf("hs_sd %.3f\n", compare_6d, axis::regular<>, axis::regular<>, axis::regular<>, axis::regular<>, axis::regular<>>, - adaptive_storage>>(6000000, itype)); + adaptive_storage>>(nfill, itype)); printf("hd_ss %.3f\n", compare_6d>>( - 6000000, itype)); + nfill, itype)); printf("hd_sd %.3f\n", compare_6d>( - 6000000, itype)); + nfill, itype)); } } diff --git a/test/speed_gsl.cpp b/test/speed_gsl.cpp index 564586a0..f6bb0357 100644 --- a/test/speed_gsl.cpp +++ b/test/speed_gsl.cpp @@ -12,28 +12,28 @@ #include #include #include -#include +#include -std::vector random_array(unsigned n, int type) { - std::vector result(n); +std::unique_ptr random_array(unsigned n, int type) { + std::unique_ptr r(new double[n]); std::default_random_engine gen(1); if (type) { // type == 1 std::normal_distribution<> d(0.5, 0.3); - for (auto &x : result) - x = d(gen); + for (unsigned i = 0; i < n; ++i) + r[i] = d(gen); } else { // type == 0 std::uniform_real_distribution<> d(0.0, 1.0); - for (auto &x : result) - x = d(gen); + for (unsigned i = 0; i < n; ++i) + r[i] = d(gen); } - return result; + return r; } void compare_1d(unsigned n, int distrib) { auto r = random_array(n, distrib); double best = std::numeric_limits::max(); - for (unsigned k = 0; k < 50; ++k) { + for (unsigned k = 0; k < 20; ++k) { gsl_histogram *h = gsl_histogram_alloc(100); gsl_histogram_set_ranges_uniform(h, 0, 1); auto t = clock(); @@ -50,7 +50,7 @@ void compare_2d(unsigned n, int distrib) { auto r = random_array(n, distrib); double best = std::numeric_limits::max(); - for (unsigned k = 0; k < 50; ++k) { + for (unsigned k = 0; k < 20; ++k) { gsl_histogram2d *h = gsl_histogram2d_alloc(100, 100); gsl_histogram2d_set_ranges_uniform(h, 0, 1, 0, 1); auto t = clock(); @@ -64,15 +64,16 @@ void compare_2d(unsigned n, int distrib) { } int main(int argc, char **argv) { + constexpr unsigned nfill = 6000000; printf("1D\n"); printf("uniform distribution\n"); - compare_1d(6000000, 0); + compare_1d(nfill, 0); printf("normal distribution\n"); - compare_1d(6000000, 1); + compare_1d(nfill, 1); printf("2D\n"); printf("uniform distribution\n"); - compare_2d(6000000, 0); + compare_2d(nfill, 0); printf("normal distribution\n"); - compare_2d(6000000, 1); + compare_2d(nfill, 1); } diff --git a/test/speed_numpy.py b/test/speed_numpy.py index 02b91049..5670b20d 100755 --- a/test/speed_numpy.py +++ b/test/speed_numpy.py @@ -19,7 +19,7 @@ def compare_1d(n, distrib): best_numpy = float("infinity") best_boost = float("infinity") - for k in xrange(50): + for k in xrange(20): t = timer() w, xe = np.histogram(r, bins=100, range=(0.0, 1.0)) t = timer() - t @@ -44,7 +44,7 @@ def compare_2d(n, distrib): best_numpy = float("infinity") best_boost = float("infinity") - for k in xrange(50): + for k in xrange(20): t = timer() w, xe, ye = np.histogram2d(r[0], r[1], bins=(100, 100), range=((0.0, 1.0), (0.0, 1.0))) @@ -70,7 +70,7 @@ def compare_3d(n, distrib): best_numpy = float("infinity") best_boost = float("infinity") - for k in xrange(50): + for k in xrange(20): t = timer() w, xe = np.histogramdd(r.T, bins=(100, 100, 100), range=((0.0, 1.0), @@ -100,7 +100,7 @@ def compare_6d(n, distrib): best_numpy = float("infinity") best_boost = float("infinity") - for k in xrange(50): + for k in xrange(20): t = timer() w, xe = np.histogramdd(r.T, bins=(10, 10, 10, 10, 10, 10), @@ -128,26 +128,28 @@ def compare_6d(n, distrib): print "py:numpy %.3f" % best_numpy print "py:hd_sd %.3f" % best_boost +nfill = 6000000 + print "1D" print "uniform distribution" -compare_1d(6000000, 0) +compare_1d(nfill, 0) print "normal distribution" -compare_1d(6000000, 1) +compare_1d(nfill, 1) print "2D" print "uniform distribution" -compare_2d(6000000, 0) +compare_2d(nfill, 0) print "normal distribution" -compare_2d(6000000, 1) +compare_2d(nfill, 1) print "3D" print "uniform distribution" -compare_3d(6000000, 0) +compare_3d(nfill, 0) print "normal distribution" -compare_3d(6000000, 1) +compare_3d(nfill, 1) print "6D" print "uniform distribution" -compare_6d(6000000, 0) +compare_6d(nfill, 0) print "normal distribution" -compare_6d(6000000, 1) +compare_6d(nfill, 1) diff --git a/test/speed_root.cpp b/test/speed_root.cpp index e66a48f1..06e3a424 100644 --- a/test/speed_root.cpp +++ b/test/speed_root.cpp @@ -14,28 +14,28 @@ #include #include #include -#include +#include -std::vector random_array(unsigned n, int type) { - std::vector result(n); +std::unique_ptr random_array(unsigned n, int type) { + std::unique_ptr r(new double[n]); std::default_random_engine gen(1); if (type) { // type == 1 std::normal_distribution<> d(0.5, 0.3); - for (auto &x : result) - x = d(gen); + for (unsigned i = 0; i < n; ++i) + r[i] = d(gen); } else { // type == 0 std::uniform_real_distribution<> d(0.0, 1.0); - for (auto &x : result) - x = d(gen); + for (unsigned i = 0; i < n; ++i) + r[i] = d(gen); } - return result; + return r; } void compare_1d(unsigned n, int distrib) { auto r = random_array(n, distrib); double best_root = std::numeric_limits::max(); - for (unsigned k = 0; k < 50; ++k) { + for (unsigned k = 0; k < 20; ++k) { TH1I hroot("", "", 100, 0, 1); auto t = clock(); for (unsigned i = 0; i < n; ++i) @@ -50,7 +50,7 @@ void compare_2d(unsigned n, int distrib) { auto r = random_array(n, distrib); double best_root = std::numeric_limits::max(); - for (unsigned k = 0; k < 50; ++k) { + for (unsigned k = 0; k < 20; ++k) { TH2I hroot("", "", 100, 0, 1, 100, 0, 1); auto t = clock(); for (unsigned i = 0; i < n/2; ++i) @@ -65,7 +65,7 @@ void compare_3d(unsigned n, int distrib) { auto r = random_array(n, distrib); double best_root = std::numeric_limits::max(); - for (unsigned k = 0; k < 50; ++k) { + for (unsigned k = 0; k < 20; ++k) { TH3I hroot("", "", 100, 0, 1, 100, 0, 1, 100, 0, 1); auto t = clock(); for (unsigned i = 0; i < n/3; ++i) @@ -80,7 +80,7 @@ void compare_6d(unsigned n, int distrib) { auto r = random_array(n, distrib); double best_root = std::numeric_limits::max(); - for (unsigned k = 0; k < 50; ++k) { + for (unsigned k = 0; k < 20; ++k) { std::vector bin(6, 10); std::vector min(6, 0); std::vector max(6, 1); @@ -88,7 +88,7 @@ void compare_6d(unsigned n, int distrib) { auto t = clock(); for (unsigned i = 0; i < n/6; ++i) { - hroot.Fill(&r.front() + 6 * i); + hroot.Fill(r.get() + 6 * i); } t = clock() - t; best_root = std::min(best_root, double(t) / CLOCKS_PER_SEC); @@ -97,27 +97,29 @@ void compare_6d(unsigned n, int distrib) { } int main(int argc, char **argv) { + constexpr unsigned nfill = 6000000; + printf("1D\n"); printf("uniform distribution\n"); - compare_1d(6000000, 0); + compare_1d(nfill, 0); printf("normal distribution\n"); - compare_1d(6000000, 1); + compare_1d(nfill, 1); printf("2D\n"); printf("uniform distribution\n"); - compare_2d(6000000, 0); + compare_2d(nfill, 0); printf("normal distribution\n"); - compare_2d(6000000, 1); + compare_2d(nfill, 1); printf("3D\n"); printf("uniform distribution\n"); - compare_3d(6000000, 0); + compare_3d(nfill, 0); printf("normal distribution\n"); - compare_3d(6000000, 1); + compare_3d(nfill, 1); printf("6D\n"); printf("uniform distribution\n"); - compare_6d(6000000, 0); + compare_6d(nfill, 0); printf("normal distribution\n"); - compare_6d(6000000, 1); + compare_6d(nfill, 1); }