blob: 61bd4dae65474ec620d8a36b8886e11918810637 [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#endif
14
15#include <boost/container/allocator.hpp>
16
17#define BOOST_CONTAINER_VECTOR_ALLOC_STATS
18
19#include <boost/container/vector.hpp>
20#include <memory> //std::allocator
21#include <iostream> //std::cout, std::endl
22#include <cassert> //assert
23
24#include <boost/timer/timer.hpp>
25using boost::timer::cpu_timer;
26using boost::timer::cpu_times;
27using boost::timer::nanosecond_type;
28
29namespace bc = boost::container;
30
31typedef std::allocator<int> StdAllocator;
32typedef bc::allocator<int, 2, bc::expand_bwd | bc::expand_fwd> AllocatorPlusV2Mask;
33typedef bc::allocator<int, 2, bc::expand_fwd> AllocatorPlusV2;
34typedef bc::allocator<int, 1> AllocatorPlusV1;
35
36template<class Allocator> struct get_allocator_name;
37
38template<> struct get_allocator_name<StdAllocator>
39{ static const char *get() { return "StdAllocator"; } };
40
41template<> struct get_allocator_name<AllocatorPlusV2Mask>
42{ static const char *get() { return "AllocatorPlusV2Mask"; } };
43
44template<> struct get_allocator_name<AllocatorPlusV2>
45{ static const char *get() { return "AllocatorPlusV2"; } };
46
47template<> struct get_allocator_name<AllocatorPlusV1>
48{ static const char *get() { return "AllocatorPlusV1"; } };
49
50//typedef int MyInt;
51
52class MyInt
53{
54 int int_;
55
56 public:
57 MyInt(int i = 0)
58 : int_(i)
59 {}
60
61 MyInt(const MyInt &other)
62 : int_(other.int_)
63 {}
64
65 MyInt & operator=(const MyInt &other)
66 {
67 int_ = other.int_;
68 return *this;
69 }
70
71 ~MyInt()
72 {
73 int_ = 0;
74 }
75};
76namespace boost{
77
78template<class T>
79struct has_trivial_destructor_after_move;
80
81template<>
82struct has_trivial_destructor_after_move<MyInt>
83{
84 static const bool value = true;
85 //static const bool value = false;
86};
87
88} //namespace boost{
89
90
91void print_header()
92{
93 std::cout << "Allocator" << ";" << "Iterations" << ";" << "Size" << ";"
94 << "Capacity" << ";" << "push_back(ns)" << ";" << "Allocator calls" << ";"
95 << "New allocations" << ";" << "Bwd expansions" << std::endl;
96}
97
98template<class Allocator>
99void vector_test_template(unsigned int num_iterations, unsigned int num_elements, bool csv_output)
100{
101 typedef typename Allocator::template rebind<MyInt>::other IntAllocator;
102 unsigned int numalloc = 0, numexpand = 0;
103
104 cpu_timer timer;
105 timer.resume();
106
107 unsigned int capacity = 0;
108 for(unsigned int r = 0; r != num_iterations; ++r){
109 bc::vector<MyInt, IntAllocator> v;
110 v.reset_alloc_stats();
111 void *first_mem = 0;
112 try{
113 first_mem = bc::dlmalloc_malloc(sizeof(MyInt)*num_elements*3/2);
114 v.push_back(MyInt(0));
115 bc::dlmalloc_free(first_mem);
116
117 for(unsigned int e = 0; e != num_elements; ++e){
118 v.push_back(MyInt(e));
119 }
120 numalloc += v.num_alloc;
121 numexpand += v.num_expand_bwd;
122 capacity = static_cast<unsigned int>(v.capacity());
123 }
124 catch(...){
125 bc::dlmalloc_free(first_mem);
126 throw;
127 }
128 }
129
130 assert(bc::dlmalloc_allocated_memory() == 0);
131
132 timer.stop();
133 nanosecond_type nseconds = timer.elapsed().wall;
134
135 if(csv_output){
136 std::cout << get_allocator_name<Allocator>::get()
137 << ";"
138 << num_iterations
139 << ";"
140 << num_elements
141 << ";"
142 << capacity
143 << ";"
144 << float(nseconds)/(num_iterations*num_elements)
145 << ";"
146 << (float(numalloc) + float(numexpand))/num_iterations
147 << ";"
148 << float(numalloc)/num_iterations
149 << ";"
150 << float(numexpand)/num_iterations
151 << std::endl;
152 }
153 else{
154 std::cout << std::endl
155 << "Allocator: " << get_allocator_name<Allocator>::get()
156 << std::endl
157 << " push_back ns: "
158 << float(nseconds)/(num_iterations*num_elements)
159 << std::endl
160 << " capacity - alloc calls (new/expand): "
161 << (unsigned int)capacity << " - "
162 << (float(numalloc) + float(numexpand))/num_iterations
163 << "(" << float(numalloc)/num_iterations << "/" << float(numexpand)/num_iterations << ")"
164 << std::endl;
165 std::cout << '\n'
166 << " ----------------------------------- "
167 << std::endl;
168 }
169 bc::dlmalloc_trim(0);
170}
171
172int main(int argc, const char *argv[])
173{
174 #define SINGLE_TEST
175 #ifndef SINGLE_TEST
176 #ifdef NDEBUG
177 unsigned int numit [] = { 2000, 20000, 200000, 2000000 };
178 #else
179 unsigned int numit [] = { 100, 1000, 10000, 100000 };
180 #endif
181 unsigned int numele [] = { 10000, 1000, 100, 10 };
182 #else
183 #ifdef NDEBUG
184 unsigned int numit [] = { 2000 };
185 #else
186 unsigned int numit [] = { 100 };
187 #endif
188 unsigned int numele [] = { 10000 };
189 #endif
190
191 bool csv_output = argc == 2 && (strcmp(argv[1], "--csv-output") == 0);
192
193 if(csv_output){
194 print_header();
195 for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
196 vector_test_template<StdAllocator>(numit[i], numele[i], csv_output);
197 }
198 for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
199 vector_test_template<AllocatorPlusV1>(numit[i], numele[i], csv_output);
200 }
201 for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
202 vector_test_template<AllocatorPlusV2Mask>(numit[i], numele[i], csv_output);
203 }
204 for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
205 vector_test_template<AllocatorPlusV2>(numit[i], numele[i], csv_output);
206 }
207 }
208 else{
209 for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
210 std::cout << "\n ----------------------------------- \n"
211 << " Iterations/Elements: " << numit[i] << "/" << numele[i]
212 << "\n ----------------------------------- \n";
213 vector_test_template<StdAllocator>(numit[i], numele[i], csv_output);
214 vector_test_template<AllocatorPlusV1>(numit[i], numele[i], csv_output);
215 vector_test_template<AllocatorPlusV2Mask>(numit[i], numele[i], csv_output);
216 vector_test_template<AllocatorPlusV2>(numit[i], numele[i], csv_output);
217 }
218 }
219 return 0;
220}