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