blob: 2293a0b80c78f117fbd21d0346483fc277f9c8f9 [file] [log] [blame]
Brian Silvermanfad8f552018-08-04 23:36:19 -07001//////////////////////////////////////////////////////////////////////////////
2//
3// (C) Copyright Ion Gaztanaga 2007-2013. 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//
7// See http://www.boost.org/libs/container for documentation.
8//
9//////////////////////////////////////////////////////////////////////////////
10
11#ifdef _MSC_VER
12#pragma warning (disable : 4512)
13#pragma warning (disable : 4267)
14#pragma warning (disable : 4244)
15#endif
16
17#define BOOST_CONTAINER_VECTOR_ALLOC_STATS
18
19#include <boost/container/allocator.hpp>
20#include <vector>
21#include <boost/container/vector.hpp>
22
23#include <memory> //std::allocator
24#include <iostream> //std::cout, std::endl
25#include <cstring> //std::strcmp
26#include <boost/timer/timer.hpp>
27#include <typeinfo>
28using boost::timer::cpu_timer;
29using boost::timer::cpu_times;
30using boost::timer::nanosecond_type;
31
32namespace bc = boost::container;
33
34#if defined(BOOST_CONTAINER_VECTOR_ALLOC_STATS)
35
36template<class T, class Allocator>
37static void reset_alloc_stats(std::vector<T, Allocator> &)
38 {}
39
40template<class T, class Allocator>
41static std::size_t get_num_alloc(std::vector<T, Allocator> &)
42 { return 0; }
43
44template<class T, class Allocator>
45static std::size_t get_num_expand(std::vector<T, Allocator> &)
46 { return 0; }
47
48template<class T, class Allocator>
49static void reset_alloc_stats(bc::vector<T, Allocator> &v)
50 { v.reset_alloc_stats(); }
51
52template<class T, class Allocator>
53static std::size_t get_num_alloc(bc::vector<T, Allocator> &v)
54 { return v.num_alloc; }
55
56template<class T, class Allocator>
57static std::size_t get_num_expand(bc::vector<T, Allocator> &v)
58 { return v.num_expand_fwd; }
59
60#endif //BOOST_CONTAINER_VECTOR_ALLOC_STATS
61
62class MyInt
63{
64 int int_;
65
66 public:
67 explicit MyInt(int i = 0)
68 : int_(i)
69 {}
70
71 MyInt(const MyInt &other)
72 : int_(other.int_)
73 {}
74
75 MyInt & operator=(const MyInt &other)
76 {
77 int_ = other.int_;
78 return *this;
79 }
80
81 ~MyInt()
82 {
83 int_ = 0;
84 }
85};
86
87template<class Container>
88void vector_test_template(unsigned int num_iterations, unsigned int num_elements)
89{
90 unsigned int numalloc = 0, numexpand = 0;
91
92 cpu_timer timer;
93 timer.resume();
94
95 unsigned int capacity = 0;
96 for(unsigned int r = 0; r != num_iterations; ++r){
97 Container v;
98 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
99 reset_alloc_stats(v);
100 #endif
101 //v.reserve(num_elements);
102 //MyInt a[3];
103/*
104 for(unsigned int e = 0; e != num_elements/3; ++e){
105 v.insert(v.end(), &a[0], &a[0]+3);
106 }*/
107/*
108 for(unsigned int e = 0; e != num_elements/3; ++e){
109 v.insert(v.end(), 3, MyInt(e));
110 }*/
111/*
112 for(unsigned int e = 0; e != num_elements/3; ++e){
113 v.insert(v.empty() ? v.end() : --v.end(), &a[0], &a[0]+3);
114 }*/
115/*
116 for(unsigned int e = 0; e != num_elements/3; ++e){
117 v.insert(v.empty() ? v.end() : --v.end(), 3, MyInt(e));
118 }*/
119/*
120 for(unsigned int e = 0; e != num_elements/3; ++e){
121 v.insert(v.size() >= 3 ? v.end()-3 : v.begin(), &a[0], &a[0]+3);
122 }*/
123/*
124 for(unsigned int e = 0; e != num_elements/3; ++e){
125 v.insert(v.size() >= 3 ? v.end()-3 : v.begin(), 3, MyInt(e));
126 }*/
127/*
128 for(unsigned int e = 0; e != num_elements; ++e){
129 v.insert(v.end(), MyInt(e));
130 }*/
131/*
132 for(unsigned int e = 0; e != num_elements; ++e){
133 v.insert(v.empty() ? v.end() : --v.end(), MyInt(e));
134 }*/
135
136 for(unsigned int e = 0; e != num_elements; ++e){
137 v.push_back(MyInt(e));
138 }
139
140 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
141 numalloc += get_num_alloc(v);
142 numexpand += get_num_expand(v);
143 #endif
144 capacity = static_cast<unsigned int>(v.capacity());
145 }
146
147 timer.stop();
148 nanosecond_type nseconds = timer.elapsed().wall;
149
150 std::cout << std::endl
151 << "Allocator: " << typeid(typename Container::allocator_type).name()
152 << std::endl
153 << " push_back ns: "
154 << float(nseconds)/(num_iterations*num_elements)
155 << std::endl
156 << " capacity - alloc calls (new/expand): "
157 << (unsigned int)capacity << " - "
158 << (float(numalloc) + float(numexpand))/num_iterations
159 << "(" << float(numalloc)/num_iterations << "/" << float(numexpand)/num_iterations << ")"
160 << std::endl << std::endl;
161 bc::dlmalloc_trim(0);
162}
163
164void print_header()
165{
166 std::cout << "Allocator" << ";" << "Iterations" << ";" << "Size" << ";"
167 << "Capacity" << ";" << "push_back(ns)" << ";" << "Allocator calls" << ";"
168 << "New allocations" << ";" << "Fwd expansions" << std::endl;
169}
170
171int main()
172{
173 #define SINGLE_TEST
174 #ifndef SINGLE_TEST
175 #ifdef NDEBUG
176 unsigned int numit [] = { 1000, 10000, 100000, 1000000 };
177 #else
178 unsigned int numit [] = { 100, 1000, 10000, 100000 };
179 #endif
180 unsigned int numele [] = { 10000, 1000, 100, 10 };
181 #else
182 #ifdef NDEBUG
183 std::size_t numit [] = { 1000 };
184 #else
185 std::size_t numit [] = { 100 };
186 #endif
187 std::size_t numele [] = { 10000 };
188 #endif
189
190 print_header();
191 for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
192 vector_test_template< bc::vector<MyInt, std::allocator<MyInt> > >(numit[i], numele[i]);
193 vector_test_template< bc::vector<MyInt, bc::allocator<MyInt, 1> > >(numit[i], numele[i]);
194 vector_test_template<bc::vector<MyInt, bc::allocator<MyInt, 2, bc::expand_bwd | bc::expand_fwd> > >(numit[i], numele[i]);
195 vector_test_template<bc::vector<MyInt, bc::allocator<MyInt, 2> > >(numit[i], numele[i]);
196 }
197 return 0;
198}