mirror of
https://github.com/boostorg/histogram.git
synced 2025-05-11 05:07:58 +00:00
215 lines
5.4 KiB
C++
215 lines
5.4 KiB
C++
// Copyright 2015-2016 Hans Dembinski
|
|
//
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
// (See accompanying file LICENSE_1_0.txt
|
|
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
#include <boost/histogram/static_histogram.hpp>
|
|
#include <boost/histogram/dynamic_histogram.hpp>
|
|
#include <boost/histogram/storage/container_storage.hpp>
|
|
#include <boost/histogram/storage/adaptive_storage.hpp>
|
|
#include <boost/histogram/axis.hpp>
|
|
|
|
#include <random>
|
|
#include <algorithm>
|
|
#include <limits>
|
|
#include <vector>
|
|
#include <ctime>
|
|
#include <cstdio>
|
|
|
|
using namespace boost::histogram;
|
|
namespace mpl = boost::mpl;
|
|
|
|
std::vector<double> random_array(unsigned n, int type) {
|
|
std::vector<double> result(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);
|
|
}
|
|
else { // type == 0
|
|
std::uniform_real_distribution<> d(0.0, 1.0);
|
|
for (auto& x: result)
|
|
x = d(gen);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
template <typename Histogram>
|
|
double compare_1d(unsigned n, int distrib)
|
|
{
|
|
auto r = random_array(n, distrib);
|
|
|
|
auto best = std::numeric_limits<double>::max();
|
|
for (unsigned k = 0; k < 10; ++k) {
|
|
auto h = Histogram(regular_axis(100, 0, 1));
|
|
auto t = clock();
|
|
for (unsigned i = 0; i < n; ++i)
|
|
h.increment(r[i]);
|
|
t = clock() - t;
|
|
best = std::min(best, double(t) / CLOCKS_PER_SEC);
|
|
}
|
|
|
|
return best;
|
|
}
|
|
|
|
template <typename Histogram>
|
|
double compare_3d(unsigned n, int distrib)
|
|
{
|
|
auto r = random_array(3 * n, distrib);
|
|
|
|
auto best = std::numeric_limits<double>::max();
|
|
for (unsigned k = 0; k < 10; ++k) {
|
|
auto h = Histogram(regular_axis(100, 0, 1),
|
|
regular_axis(100, 0, 1),
|
|
regular_axis(100, 0, 1));
|
|
auto t = clock();
|
|
for (unsigned i = 0; i < n; ++i)
|
|
h.increment(r[3 * i], r[3 * i + 1], r[3 * i + 2]);
|
|
t = clock() - t;
|
|
best = std::min(best, double(t) / CLOCKS_PER_SEC);
|
|
}
|
|
|
|
return best;
|
|
}
|
|
|
|
template <typename Histogram>
|
|
double compare_6d(unsigned n, int distrib)
|
|
{
|
|
auto r = random_array(6 * n, distrib);
|
|
|
|
auto best = std::numeric_limits<double>::max();
|
|
for (unsigned k = 0; k < 10; ++k) {
|
|
double x[6];
|
|
|
|
auto h = Histogram(regular_axis(10, 0, 1),
|
|
regular_axis(10, 0, 1),
|
|
regular_axis(10, 0, 1),
|
|
regular_axis(10, 0, 1),
|
|
regular_axis(10, 0, 1),
|
|
regular_axis(10, 0, 1));
|
|
|
|
auto t = clock();
|
|
for (unsigned i = 0; i < n; ++i) {
|
|
for (unsigned k = 0; k < 6; ++k)
|
|
x[k] = r[6 * i + k];
|
|
h.increment(x[0], x[1], x[2], x[3], x[4], x[5]);
|
|
}
|
|
t = clock() - t;
|
|
best = std::min(best, double(t) / CLOCKS_PER_SEC);
|
|
}
|
|
|
|
return best;
|
|
}
|
|
|
|
int main() {
|
|
for (int itype = 0; itype < 2; ++itype) {
|
|
if (itype == 0)
|
|
printf("uniform distribution\n");
|
|
else
|
|
printf("normal distribution\n");
|
|
|
|
printf("1D\n");
|
|
printf("t[boost] = %.3f\n",
|
|
#if HISTOGRAM_TYPE == 1
|
|
compare_1d<
|
|
static_histogram<
|
|
mpl::vector<regular_axis>,
|
|
container_storage<std::vector<int>>
|
|
>
|
|
>(12000000, itype)
|
|
#elif HISTOGRAM_TYPE == 2
|
|
compare_1d<
|
|
static_histogram<
|
|
mpl::vector<regular_axis>,
|
|
adaptive_storage
|
|
>
|
|
>(12000000, itype)
|
|
#elif HISTOGRAM_TYPE == 3
|
|
compare_1d<
|
|
dynamic_histogram<
|
|
default_axes,
|
|
container_storage<std::vector<int>>
|
|
>
|
|
>(12000000, itype)
|
|
#elif HISTOGRAM_TYPE == 4
|
|
compare_1d<
|
|
dynamic_histogram<
|
|
default_axes,
|
|
adaptive_storage
|
|
>
|
|
>(12000000, itype)
|
|
#endif
|
|
);
|
|
|
|
printf("3D\n");
|
|
printf("t[boost] = %.3f\n",
|
|
#if HISTOGRAM_TYPE == 1
|
|
compare_3d<
|
|
static_histogram<
|
|
mpl::vector<regular_axis, regular_axis, regular_axis>,
|
|
container_storage<std::vector<int>>
|
|
>
|
|
>(4000000, itype)
|
|
#elif HISTOGRAM_TYPE == 2
|
|
compare_3d<
|
|
static_histogram<
|
|
mpl::vector<regular_axis, regular_axis, regular_axis>,
|
|
adaptive_storage
|
|
>
|
|
>(4000000, itype)
|
|
#elif HISTOGRAM_TYPE == 3
|
|
compare_3d<
|
|
dynamic_histogram<
|
|
default_axes,
|
|
container_storage<std::vector<int>>
|
|
>
|
|
>(4000000, itype)
|
|
#elif HISTOGRAM_TYPE == 4
|
|
compare_3d<
|
|
dynamic_histogram<
|
|
default_axes,
|
|
adaptive_storage
|
|
>
|
|
>(4000000, itype)
|
|
#endif
|
|
);
|
|
|
|
printf("6D\n");
|
|
printf("t[boost] = %.3f\n",
|
|
#if HISTOGRAM_TYPE == 1
|
|
compare_6d<
|
|
static_histogram<
|
|
mpl::vector<regular_axis, regular_axis, regular_axis,
|
|
regular_axis, regular_axis, regular_axis>,
|
|
container_storage<std::vector<int>>
|
|
>
|
|
>(2000000, itype)
|
|
#elif HISTOGRAM_TYPE == 2
|
|
compare_6d<
|
|
static_histogram<
|
|
mpl::vector<regular_axis, regular_axis, regular_axis,
|
|
regular_axis, regular_axis, regular_axis>,
|
|
adaptive_storage
|
|
>
|
|
>(2000000, itype)
|
|
#elif HISTOGRAM_TYPE == 3
|
|
compare_6d<
|
|
dynamic_histogram<
|
|
default_axes,
|
|
container_storage<std::vector<int>>
|
|
>
|
|
>(2000000, itype)
|
|
#elif HISTOGRAM_TYPE == 4
|
|
compare_6d<
|
|
dynamic_histogram<
|
|
default_axes,
|
|
adaptive_storage
|
|
>
|
|
>(2000000, itype)
|
|
#endif
|
|
);
|
|
}
|
|
}
|