faster with static_vector

This commit is contained in:
Hans Dembinski 2016-04-09 17:27:03 -04:00
parent fbe82e3c58
commit d2306810ee
6 changed files with 21 additions and 15 deletions

View File

@ -43,7 +43,7 @@ void compare_1d(unsigned n)
double best_root = std::numeric_limits<double>::max(); double best_root = std::numeric_limits<double>::max();
double best_boost = std::numeric_limits<double>::max(); double best_boost = std::numeric_limits<double>::max();
for (unsigned k = 0; k < 3; ++k) { for (unsigned k = 0; k < 10; ++k) {
TH1I hroot("", "", 100, 0, 1); TH1I hroot("", "", 100, 0, 1);
clock_t t = clock(); clock_t t = clock();
for (unsigned i = 0; i < n; ++i) for (unsigned i = 0; i < n; ++i)
@ -72,7 +72,7 @@ void compare_3d(unsigned n)
double best_root = std::numeric_limits<double>::max(); double best_root = std::numeric_limits<double>::max();
double best_boost = std::numeric_limits<double>::max(); double best_boost = std::numeric_limits<double>::max();
for (unsigned k = 0; k < 3; ++k) { for (unsigned k = 0; k < 10; ++k) {
TH3I hroot("", "", 100, 0, 1, 100, 0, 1, 100, 0, 1); TH3I hroot("", "", 100, 0, 1, 100, 0, 1, 100, 0, 1);
clock_t t = clock(); clock_t t = clock();
for (unsigned i = 0; i < n; ++i) for (unsigned i = 0; i < n; ++i)
@ -102,7 +102,7 @@ void compare_6d(unsigned n)
double best_root = std::numeric_limits<double>::max(); double best_root = std::numeric_limits<double>::max();
double best_boost = std::numeric_limits<double>::max(); double best_boost = std::numeric_limits<double>::max();
for (unsigned k = 0; k < 3; ++k) { for (unsigned k = 0; k < 10; ++k) {
double x[6]; double x[6];
vector<int> bin(6, 10); vector<int> bin(6, 10);
vector<double> min(6, 0); vector<double> min(6, 0);

View File

@ -248,8 +248,6 @@ typedef variant<
integer_axis integer_axis
> axis_type; > axis_type;
typedef std::vector<axis_type> axes_type;
std::ostream& operator<<(std::ostream&, const axis_type&); std::ostream& operator<<(std::ostream&, const axis_type&);
} }

View File

@ -172,7 +172,7 @@ private:
ar & data_; ar & data_;
} }
friend class buffer_access; friend class histogram_access;
}; };
histogram operator+(const histogram& a, const histogram& b) { histogram operator+(const histogram& a, const histogram& b) {

View File

@ -9,6 +9,7 @@
#include <boost/serialization/split_member.hpp> #include <boost/serialization/split_member.hpp>
#include <boost/serialization/variant.hpp> #include <boost/serialization/variant.hpp>
#include <boost/serialization/vector.hpp> #include <boost/serialization/vector.hpp>
#include <boost/container/static_vector.hpp>
#include <boost/type_traits/is_same.hpp> #include <boost/type_traits/is_same.hpp>
#include <bitset> #include <bitset>
@ -21,7 +22,8 @@ namespace histogram {
// holds an array of axis and computes the internal index // holds an array of axis and computes the internal index
class histogram_base { class histogram_base {
public: public:
typedef uint64_t size_type; typedef container::static_vector<axis_type, BOOST_HISTOGRAM_AXIS_LIMIT> axes_type;
typedef uintptr_t size_type;
histogram_base(const histogram_base&); histogram_base(const histogram_base&);
histogram_base& operator=(const histogram_base&); histogram_base& operator=(const histogram_base&);
@ -55,7 +57,7 @@ protected:
update_buffers(); \ update_buffers(); \
} }
// generates constructors taking 2 to AXIS_LIMIT arguments // generates constructors taking 1 to AXIS_LIMIT arguments
BOOST_PP_REPEAT_FROM_TO(1, BOOST_HISTOGRAM_AXIS_LIMIT, BOOST_HISTOGRAM_BASE_CTOR, nil) BOOST_PP_REPEAT_FROM_TO(1, BOOST_HISTOGRAM_AXIS_LIMIT, BOOST_HISTOGRAM_BASE_CTOR, nil)
bool operator==(const histogram_base&) const; bool operator==(const histogram_base&) const;
@ -106,9 +108,15 @@ private:
void serialize(Archive& ar, unsigned version) void serialize(Archive& ar, unsigned version)
{ {
using namespace serialization; using namespace serialization;
ar & axes_; unsigned size = axes_.size();
if (Archive::is_loading::value) ar & size;
if (Archive::is_loading::value) {
axes_.resize(size);
ar & serialization::make_array(&axes_[0], size);
update_buffers(); update_buffers();
} else {
ar & serialization::make_array(&axes_[0], size);
}
} }
}; };

View File

@ -29,7 +29,7 @@ histogram_init(python::tuple args, python::dict kwargs) {
} }
// normal constructor // normal constructor
axes_type axes; histogram_base::axes_type axes;
for (unsigned i = 1, n = len(args); i < n; ++i) { for (unsigned i = 1, n = len(args); i < n; ++i) {
object pa = args[i]; object pa = args[i];
extract<regular_axis> er(pa); extract<regular_axis> er(pa);
@ -135,7 +135,7 @@ histogram_getitem(const histogram& self, python::object oidx) {
return self.value(self.dim(), idx); return self.value(self.dim(), idx);
} }
class buffer_access { class histogram_access {
public: public:
static static
python::dict python::dict
@ -161,8 +161,8 @@ void register_histogram()
>("histogram", no_init) >("histogram", no_init)
.def("__init__", raw_function(histogram_init)) .def("__init__", raw_function(histogram_init))
// shadowed C++ ctors // shadowed C++ ctors
.def(init<const axes_type&>()) .def(init<const histogram_base::axes_type&>())
.add_property("__array_interface__", &buffer_access::histogram_array_interface) .add_property("__array_interface__", &histogram_access::histogram_array_interface)
.def("fill", raw_function(histogram_fill)) .def("fill", raw_function(histogram_fill))
.add_property("depth", &histogram::depth) .add_property("depth", &histogram::depth)
.add_property("sum", &histogram::sum) .add_property("sum", &histogram::sum)

View File

@ -275,7 +275,7 @@ void register_histogram_base() {
// used to pass arguments from raw python init to specialized C++ constructors // used to pass arguments from raw python init to specialized C++ constructors
class_<std::vector<double> >("vector_double", no_init); class_<std::vector<double> >("vector_double", no_init);
class_<std::vector<std::string> >("vector_string", no_init); class_<std::vector<std::string> >("vector_string", no_init);
class_<axes_type>("axes", no_init); class_<histogram_base::axes_type>("axes", no_init);
class_<regular_axis>("regular_axis", no_init) class_<regular_axis>("regular_axis", no_init)
.def(init<unsigned, double, double, std::string, bool>( .def(init<unsigned, double, double, std::string, bool>(