Brian Silverman | fad8f55 | 2018-08-04 23:36:19 -0700 | [diff] [blame^] | 1 | // benchmark based on: http://cpp-next.com/archive/2010/10/howards-stl-move-semantics-benchmark/ |
| 2 | // |
| 3 | // @file bench_static_vector.cpp |
| 4 | // @date Aug 14, 2011 |
| 5 | // @author Andrew Hundt <ATHundt@gmail.com> |
| 6 | // |
| 7 | // (C) Copyright 2011-2013 Andrew Hundt <ATHundt@gmail.com> |
| 8 | // (C) Copyright 2013-2013 Ion Gaztanaga |
| 9 | // |
| 10 | // Distributed under the Boost Software License, Version 1.0. (See |
| 11 | // accompanying file LICENSE_1_0.txt or copy at |
| 12 | // http://www.boost.org/LICENSE_1_0.txt) |
| 13 | // |
| 14 | // @brief varray_benchmark.cpp compares the performance of boost::container::varray to boost::container::vector |
| 15 | |
| 16 | #include "varray.hpp" |
| 17 | #include "boost/container/vector.hpp" |
| 18 | #include "boost/container/static_vector.hpp" |
| 19 | #include "../test/movable_int.hpp" |
| 20 | #include <vector> |
| 21 | #include <iostream> |
| 22 | #include <boost/timer/timer.hpp> |
| 23 | #include <algorithm> |
| 24 | #include <exception> |
| 25 | |
| 26 | using boost::timer::cpu_timer; |
| 27 | using boost::timer::cpu_times; |
| 28 | using boost::timer::nanosecond_type; |
| 29 | |
| 30 | static const std::size_t N = 500; |
| 31 | |
| 32 | #ifdef NDEBUG |
| 33 | static const std::size_t Iter = 50; |
| 34 | #else |
| 35 | static const std::size_t Iter = 5; |
| 36 | #endif |
| 37 | |
| 38 | //#define BENCH_SIMPLE_CONSTRUCTION |
| 39 | //#define BENCH_TRIVIAL_TYPE |
| 40 | |
| 41 | #ifdef BENCH_TRIVIAL_TYPE |
| 42 | typedef std::size_t basic_type_t; |
| 43 | #else |
| 44 | typedef boost::container::test::copyable_int basic_type_t; |
| 45 | #endif |
| 46 | |
| 47 | template<typename T> |
| 48 | T &get_set(std::size_t) |
| 49 | { |
| 50 | #ifdef BENCH_SIMPLE_CONSTRUCTION |
| 51 | T &t = *new T(N); |
| 52 | for (std::size_t i = 0; i < N; ++i) |
| 53 | t[i] = basic_type_t(std::rand()); |
| 54 | #else |
| 55 | T &t = *new T; |
| 56 | t.reserve(N); |
| 57 | for (std::size_t i = 0; i < N; ++i) |
| 58 | t.push_back(basic_type_t(std::rand())); |
| 59 | #endif |
| 60 | return t; |
| 61 | } |
| 62 | |
| 63 | template<typename T> |
| 64 | T &generate() |
| 65 | { |
| 66 | T &v = *new T; |
| 67 | v.reserve(N); |
| 68 | |
| 69 | for (std::size_t i = 0; i < N; ++i){ |
| 70 | typename T::reference r = get_set<typename T::value_type>(i); |
| 71 | v.push_back(boost::move(r)); |
| 72 | delete &r; |
| 73 | } |
| 74 | return v; |
| 75 | } |
| 76 | |
| 77 | template<typename T> |
| 78 | cpu_times time_it() |
| 79 | { |
| 80 | cpu_timer sortTime,rotateTime,destructionTime; |
| 81 | sortTime.stop();rotateTime.stop();destructionTime.stop(); |
| 82 | cpu_timer totalTime, constructTime; |
| 83 | std::srand (0); |
| 84 | for(std::size_t i = 0; i< Iter; ++i){ |
| 85 | constructTime.resume(); |
| 86 | { |
| 87 | T &v = generate<T>(); |
| 88 | constructTime.stop(); |
| 89 | sortTime.resume(); |
| 90 | std::sort(v.begin(), v.end()); |
| 91 | sortTime.stop(); |
| 92 | rotateTime.resume(); |
| 93 | std::rotate(v.begin(), v.begin() + v.size()/2, v.end()); |
| 94 | rotateTime.stop(); |
| 95 | destructionTime.resume(); |
| 96 | delete &v; |
| 97 | } |
| 98 | destructionTime.stop(); |
| 99 | } |
| 100 | totalTime.stop(); |
| 101 | std::cout << " construction took " << boost::timer::format(constructTime.elapsed(), 6, "%ws wall, %ts CPU (%p%)\n"); |
| 102 | std::cout << " sort took " << boost::timer::format(sortTime.elapsed(), 6, "%ws wall, %ts CPU (%p%)\n"); |
| 103 | std::cout << " rotate took " << boost::timer::format(rotateTime.elapsed(), 6, "%ws wall, %ts CPU (%p%)\n"); |
| 104 | std::cout << " destruction took " << boost::timer::format(destructionTime.elapsed(), 6, "%ws wall, %ts CPU (%p%)\n"); |
| 105 | std::cout << " Total time = " << boost::timer::format(totalTime.elapsed(), 6, "%ws wall, %ts CPU (%p%)\n") << std::endl; |
| 106 | return totalTime.elapsed(); |
| 107 | } |
| 108 | |
| 109 | void compare_times(cpu_times time_numerator, cpu_times time_denominator){ |
| 110 | std::cout |
| 111 | << "\n wall = " << ((double)time_numerator.wall/(double)time_denominator.wall) |
| 112 | << "\n (user+sys) = " << ((double)(time_numerator.system+time_numerator.user)/(double)(time_denominator.system+time_denominator.user)) << "\n\n"; |
| 113 | } |
| 114 | |
| 115 | int main() |
| 116 | { |
| 117 | try { |
| 118 | std::cout << "N = " << N << " Iter = " << Iter << "\n\n"; |
| 119 | |
| 120 | std::cout << "varray benchmark:" << std::endl; |
| 121 | cpu_times time_varray = time_it<boost::container::varray<boost::container::varray<basic_type_t,N>,N > >(); |
| 122 | |
| 123 | std::cout << "boost::container::static_vector benchmark" << std::endl; |
| 124 | cpu_times time_boost_static_vector = time_it<boost::container::static_vector<boost::container::static_vector<basic_type_t,N>,N > >(); |
| 125 | |
| 126 | std::cout << "boost::container::vector benchmark" << std::endl; |
| 127 | cpu_times time_boost_vector = time_it<boost::container::vector<boost::container::vector<basic_type_t> > >(); |
| 128 | |
| 129 | std::cout << "std::vector benchmark" << std::endl; |
| 130 | cpu_times time_standard_vector = time_it<std::vector<std::vector<basic_type_t> > >(); |
| 131 | |
| 132 | std::cout << "varray/boost::container::vector total time comparison:"; |
| 133 | compare_times(time_varray, time_boost_vector); |
| 134 | |
| 135 | std::cout << "varray/boost::container::static_vector total time comparison:"; |
| 136 | compare_times(time_varray, time_boost_static_vector); |
| 137 | |
| 138 | std::cout << "varray/std::vector total time comparison:"; |
| 139 | compare_times(time_varray,time_standard_vector); |
| 140 | }catch(std::exception e){ |
| 141 | std::cout << e.what(); |
| 142 | } |
| 143 | return 0; |
| 144 | } |