//////////////////////////////////////////////////////////////////////////////// // Copyright (c) 2010-2011 Bryce Lelbach // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file BOOST_LICENSE_1_0.rst or copy at http://www.boost.org/LICENSE_1_0.txt) //////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include #include #include #include const boost::uint64_t iterations = 8; const boost::chrono::seconds limit(8); boost::uint64_t counter = 0; // {{{ types struct count { count (void) { ++counter; } }; typedef boost::shared_ptr count_ptr; typedef boost::accumulators::accumulator_set< boost::uint64_t, boost::accumulators::stats< boost::accumulators::tag::min, boost::accumulators::tag::max, boost::accumulators::tag::mean(boost::accumulators::immediate), boost::accumulators::tag::sum, boost::accumulators::tag::moment<2> > > accumulator_set; typedef boost::chrono::high_resolution_clock::duration duration; typedef boost::chrono::seconds seconds; // }}} duration now (void) { return boost::chrono::high_resolution_clock::now().time_since_epoch(); } void call_make_shared (void) { count_ptr* ptrs = (count_ptr*) ::operator new[] (1 << 30); duration begin = now(); while ((now() - begin) < limit) { if (__builtin_expect(counter > (1 << 30), false)) { std::cerr << "ran out of space\n"; ::abort(); } new (&ptrs[counter]) count_ptr(boost::make_shared()); } ::operator delete[] ((void*) ptrs); } void call_new (void) { count_ptr* ptrs = (count_ptr*) ::operator new[] (1 << 30); duration begin = now(); while ((now() - begin) < limit) { if (__builtin_expect(counter > (1 << 30), false)) { std::cerr << "ran out of space\n"; ::abort(); } new (&ptrs[counter]) count_ptr(new count); } ::operator delete[] ((void*) ptrs); } int main (void) { std::vector make_shared_counts, new_counts; for (unsigned i = 0; i < iterations; ++i) { call_make_shared(); make_shared_counts.push_back(counter); counter = 0; } for (unsigned i = 0; i < iterations; ++i) { call_new(); new_counts.push_back(counter); counter = 0; } accumulator_set make_shared_stats, new_stats; make_shared_stats = std::for_each (make_shared_counts.begin(), make_shared_counts.end(), make_shared_stats); new_stats = std::for_each (new_counts.begin(), new_counts.end(), new_stats); std::cout << "make_shared:" << "\n min = " << (boost::accumulators::min)(make_shared_stats) << "\n max = " << (boost::accumulators::max)(make_shared_stats) << "\n mean = " << boost::accumulators::mean(make_shared_stats) << "\n count = " << boost::accumulators::count(make_shared_stats) << "\n sum = " << boost::accumulators::sum(make_shared_stats) << "\n moment<2> = " << boost::accumulators::moment<2>(make_shared_stats) << "\nnew:" << "\n min = " << (boost::accumulators::min)(new_stats) << "\n max = " << (boost::accumulators::max)(new_stats) << "\n mean = " << boost::accumulators::mean(new_stats) << "\n count = " << boost::accumulators::count(new_stats) << "\n sum = " << boost::accumulators::sum(new_stats) << "\n moment<2> = " << boost::accumulators::moment<2>(new_stats) << std::endl; }