#include "bench_framework.hpp" #include #include #include #include #include #include #include #if BOOST_VERSION >= 105400 #include #endif // http://stackoverflow.com/questions/17347254/why-is-allocation-and-deallocation-of-stdvector-slower-than-dynamic-array-on-m #define FULL_ZERO_CHECK inline void ensure_zero(uint8_t * data, uint32_t size) { #ifdef FULL_ZERO_CHECK for (std::size_t i=0;i array_; test1(mapnik::parameters const& params) : test_case(params), size_(*params.get("size",256*256)), array_(size_,0) { } bool validate() const { return true; } bool operator()() const { for (std::size_t i=0;i array_; test1b(mapnik::parameters const& params) : test_case(params), size_(*params.get("size",256*256)), array_(size_,0) { } bool validate() const { return true; } bool operator()() const { for (std::size_t i=0;i array_; test1c(mapnik::parameters const& params) : test_case(params), size_(*params.get("size",256*256)), array_(size_,0) { } bool validate() const { return true; } bool operator()() const { for (std::size_t i=0;i(::operator new(sizeof(uint8_t) * size_)); std::fill(data,data + size_,0); ensure_zero(data,size_); ::operator delete(data); } return true; } }; class test2 : public benchmark::test_case { public: uint32_t size_; std::vector array_; test2(mapnik::parameters const& params) : test_case(params), size_(*params.get("size",256*256)), array_(size_,0) { } bool validate() const { return true; } bool operator()() const { for (std::size_t i=0;i(::operator new(sizeof(uint8_t)*size_)); memcpy(data, &array_[0], size_); ensure_zero(data,size_); ::operator delete(data),data=0; } return true; } }; class test3 : public benchmark::test_case { public: uint32_t size_; std::vector array_; test3(mapnik::parameters const& params) : test_case(params), size_(*params.get("size",256*256)), array_(size_,0) { } bool validate() const { return true; } bool operator()() const { for (std::size_t i=0;i data(size_); ensure_zero(&data[0],data.size()); } return true; } }; class test3b : public benchmark::test_case { public: uint32_t size_; std::vector array_; test3b(mapnik::parameters const& params) : test_case(params), size_(*params.get("size",256*256)), array_(size_,0) { } bool validate() const { return true; } bool operator()() const { for (std::size_t i=0;i data(0); data.resize(size_,0); ensure_zero(&data[0],data.size()); } return true; } }; class test3c : public benchmark::test_case { public: uint32_t size_; std::vector array_; test3c(mapnik::parameters const& params) : test_case(params), size_(*params.get("size",256*256)), array_(size_,0) { } bool validate() const { return true; } bool operator()() const { for (std::size_t i=0;i data(0); data.assign(size_,0); ensure_zero(&data[0],data.size()); } return true; } }; class test3d : public benchmark::test_case { public: uint32_t size_; std::vector array_; test3d(mapnik::parameters const& params) : test_case(params), size_(*params.get("size",256*256)), array_(size_,0) { } bool validate() const { return true; } bool operator()() const { for (std::size_t i=0;i data(size_); for (std::size_t i=0;i array_; test4(mapnik::parameters const& params) : test_case(params), size_(*params.get("size",256*256)), array_(size_,0) { } bool validate() const { return true; } bool operator()() const { for (std::size_t i=0;i array_; test5(mapnik::parameters const& params) : test_case(params), size_(*params.get("size",256*256)), array_(size_,0) { } bool validate() const { return true; } bool operator()() const { for (std::size_t i=0;i array_; test5b(mapnik::parameters const& params) : test_case(params), size_(*params.get("size",256*256)), array_(size_,0) { } bool validate() const { return true; } bool operator()() const { for (std::size_t i=0;i // http://isocpp.org/blog/2013/04/trip-report-iso-c-spring-2013-meeting // http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20130909/088700.html // http://stackoverflow.com/questions/17303902/any-alternative-to-stddynarray-presently-available class test6 : public benchmark::test_case { public: uint32_t size_; std::vector array_; test6(mapnik::parameters const& params) : test_case(params), size_(*params.get("size",256*256)), array_(size_,0) { } bool validate() const { return true; } bool operator()() const { for (std::size_t i=0;i data(static_cast(0),static_cast(size_)); ensure_zero(&data[0],size_); } return true; } }; #if BOOST_VERSION >= 105400 // http://i42.co.uk/stuff/vecarray.htm // http://www.boost.org/doc/libs/1_54_0/doc/html/boost/container/static_vector.html class test7 : public benchmark::test_case { public: uint32_t size_; std::vector array_; test7(mapnik::parameters const& params) : test_case(params), size_(*params.get("size",256*256)), array_(size_,0) { } bool validate() const { return true; } bool operator()() const { for (std::size_t i=0;i data(size_,0); ensure_zero(&data[0],size_); } return true; } }; #endif int main(int argc, char** argv) { return benchmark::sequencer(argc, argv) .run("calloc") .run("malloc/memcpy") .run("malloc/memset") .run("operator new/std::fill") .run("operator new/memcpy") .run("vector(N)") .run("vector/resize") .run("vector/assign") .run("deque(N)") .run("std::string range") .run("std::string &[0]") .run("valarray") #if BOOST_VERSION >= 105400 .run("static_vector") #endif .done(); }