Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libstdc++-v3 / testsuite / performance / 20_util / allocator / insert.cc
blobab53afcac6339eeb24f7a6ee192177ac86916fd9
1 // Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 2, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING. If not, write to the Free
16 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 // USA.
19 // As a special exception, you may use this file as part of a free software
20 // library without restriction. Specifically, if other files instantiate
21 // templates or use macros or inline functions from this file, or you compile
22 // this file and link it with other files to produce an executable, this
23 // file does not by itself cause the resulting executable to be covered by
24 // the GNU General Public License. This exception does not however
25 // invalidate any other reasons why the executable file might be covered by
26 // the GNU General Public License.
29 * The goal with this application is to compare the performance
30 * between different std::allocator implementations. The results are
31 * influenced by the underlying allocator in the "C" library, malloc.
34 // 2003-02-05 Stefan Olsson <stefan@snon.net>
36 #include <vector>
37 #include <list>
38 #include <map>
39 #include <deque>
40 #include <set>
41 #include <typeinfo>
42 #include <sstream>
43 #include <ext/mt_allocator.h>
44 #include <ext/new_allocator.h>
45 #include <ext/malloc_allocator.h>
46 #include <ext/bitmap_allocator.h>
47 #include <ext/pool_allocator.h>
48 #include <cxxabi.h>
49 #include <testsuite_performance.h>
51 using namespace std;
53 typedef int test_type;
55 // The number of iterations to be performed.
56 int iterations = 10000;
58 // The number of values to insert in the container, 32 will cause 5
59 // (re)allocations to be performed (sizes 4, 8, 16, 32 and 64)
60 // This means that all allocations are within _MAX_BYTES = 128 as
61 // defined in stl_alloc.h for __pool_alloc. Whether or not this
62 // value is relevant in "the real world" or not I don't know and
63 // should probably be investigated in more detail.
64 int insert_values = 128;
66 template<typename TestType>
67 struct value_type : public pair<TestType, TestType>
69 value_type() : pair<TestType, TestType>(0, 0) { }
71 inline value_type operator++() { return ++this->first, *this; }
72 inline operator TestType() const { return this->first; }
75 template<typename Container>
76 void
77 do_loop()
79 Container obj;
80 int test_iterations = 0;
81 value_type<test_type> test_value;
82 while (test_iterations < iterations)
84 for (int j = 0; j < insert_values; ++j)
85 obj.insert(obj.end(), ++test_value);
86 ++test_iterations;
90 template<typename Container>
91 void*
92 do_test(void* p = NULL)
94 do_loop<Container>();
95 return p;
98 template<typename Container>
99 void
100 test_container(Container obj, bool run_threaded = false)
102 using namespace __gnu_test;
103 int status;
105 time_counter time;
106 resource_counter resource;
108 start_counters(time, resource);
109 if (!run_threaded)
111 do_loop<Container>();
113 else
115 #if defined (_GLIBCXX_GCC_GTHR_POSIX_H) && !defined (NOTHREAD)
116 pthread_t t1, t2, t3, t4;
117 pthread_create(&t1, 0, &do_test<Container>, 0);
118 pthread_create(&t2, 0, &do_test<Container>, 0);
119 pthread_create(&t3, 0, &do_test<Container>, 0);
120 pthread_create(&t4, 0, &do_test<Container>, 0);
122 pthread_join(t1, NULL);
123 pthread_join(t2, NULL);
124 pthread_join(t3, NULL);
125 pthread_join(t4, NULL);
126 #endif
128 stop_counters(time, resource);
130 std::ostringstream comment;
131 if (run_threaded)
132 comment << "4-way threaded iterations: " << iterations*4 << '\t';
133 else
134 comment << "iterations: " << iterations << '\t';
135 comment << "type: " << abi::__cxa_demangle(typeid(obj).name(),
136 0, 0, &status);
137 report_header(__FILE__, comment.str());
138 report_performance(__FILE__, string(), time, resource);
142 // http://gcc.gnu.org/ml/libstdc++/2001-05/msg00105.html
143 // http://gcc.gnu.org/ml/libstdc++/2003-05/msg00231.html
144 int main(void)
146 typedef __gnu_cxx::malloc_allocator<test_type> m_alloc_type;
147 typedef __gnu_cxx::new_allocator<test_type> n_alloc_type;
148 typedef __gnu_cxx::__mt_alloc<test_type> so_alloc_type;
149 typedef __gnu_cxx::bitmap_allocator<test_type> bit_alloc_type;
150 typedef __gnu_cxx::__pool_alloc<test_type> po_alloc_type;
152 #ifdef TEST_B0
153 test_container(vector<test_type, m_alloc_type>());
154 #endif
155 #ifdef TEST_B1
156 test_container(vector<test_type, n_alloc_type>());
157 #endif
158 #ifdef TEST_B2
159 test_container(vector<test_type, so_alloc_type>());
160 #endif
161 #ifdef TEST_B3
162 test_container(vector<test_type, bit_alloc_type>());
163 #endif
164 #ifdef TEST_B4
165 test_container(vector<test_type, po_alloc_type>());
166 #endif
168 #ifdef TEST_B5
169 test_container(list<test_type, m_alloc_type>());
170 #endif
171 #ifdef TEST_B6
172 test_container(list<test_type, n_alloc_type>());
173 #endif
174 #ifdef TEST_B7
175 test_container(list<test_type, so_alloc_type>());
176 #endif
177 #ifdef TEST_B8
178 test_container(list<test_type, bit_alloc_type>());
179 #endif
180 #ifdef TEST_B9
181 test_container(list<test_type, po_alloc_type>());
182 #endif
184 #ifdef TEST_B10
185 test_container(deque<test_type, m_alloc_type>());
186 #endif
187 #ifdef TEST_B11
188 test_container(deque<test_type, n_alloc_type>());
189 #endif
190 #ifdef TEST_B12
191 test_container(deque<test_type, so_alloc_type>());
192 #endif
193 #ifdef TEST_B13
194 test_container(deque<test_type, bit_alloc_type>());
195 #endif
196 #ifdef TEST_B14
197 test_container(deque<test_type, po_alloc_type>());
198 #endif
200 typedef less<test_type> compare_type;
201 typedef pair<const test_type, test_type> pair_type;
202 typedef __gnu_cxx::malloc_allocator<pair_type> m_pair_alloc_type;
203 typedef __gnu_cxx::new_allocator<pair_type> n_pair_alloc_type;
204 typedef __gnu_cxx::__mt_alloc<pair_type> so_pair_alloc_type;
205 typedef __gnu_cxx::bitmap_allocator<pair_type> bit_pair_alloc_type;
206 typedef __gnu_cxx::__pool_alloc<pair_type> po_pair_alloc_type;
208 #ifdef TEST_B15
209 test_container(map<test_type, test_type, compare_type,
210 m_pair_alloc_type>());
211 #endif
212 #ifdef TEST_B16
213 test_container(map<test_type, test_type, compare_type,
214 n_pair_alloc_type>());
215 #endif
216 #ifdef TEST_B17
217 test_container(map<test_type, test_type, compare_type,
218 so_pair_alloc_type>());
219 #endif
220 #ifdef TEST_B18
221 test_container(map<test_type, test_type, compare_type,
222 bit_pair_alloc_type>());
223 #endif
224 #ifdef TEST_B19
225 test_container(map<test_type, test_type, compare_type,
226 po_pair_alloc_type>());
227 #endif
229 #ifdef TEST_B20
230 test_container(set<test_type, compare_type, m_alloc_type>());
231 #endif
232 #ifdef TEST_B21
233 test_container(set<test_type, compare_type, n_alloc_type>());
234 #endif
235 #ifdef TEST_B22
236 test_container(set<test_type, compare_type, so_alloc_type>());
237 #endif
238 #ifdef TEST_B23
239 test_container(set<test_type, compare_type, bit_alloc_type>());
240 #endif
241 #ifdef TEST_B24
242 test_container(set<test_type, compare_type, po_alloc_type>());
243 #endif
245 #ifdef TEST_T0
246 test_container(vector<test_type, m_alloc_type>(), true);
247 #endif
248 #ifdef TEST_T1
249 test_container(vector<test_type, n_alloc_type>(), true);
250 #endif
251 #ifdef TEST_T2
252 test_container(vector<test_type, so_alloc_type>(), true);
253 #endif
254 #ifdef TEST_T3
255 test_container(vector<test_type, bit_alloc_type>(), true);
256 #endif
257 #ifdef TEST_T4
258 test_container(vector<test_type, po_alloc_type>(), true);
259 #endif
261 #ifdef TEST_T5
262 test_container(list<test_type, m_alloc_type>(), true);
263 #endif
264 #ifdef TEST_T6
265 test_container(list<test_type, n_alloc_type>(), true);
266 #endif
267 #ifdef TEST_T7
268 test_container(list<test_type, so_alloc_type>(), true);
269 #endif
270 #ifdef TEST_T8
271 test_container(list<test_type, bit_alloc_type>(), true);
272 #endif
273 #ifdef TEST_T9
274 test_container(list<test_type, po_alloc_type>(), true);
275 #endif
277 #ifdef TEST_T10
278 test_container(deque<test_type, m_alloc_type>(), true);
279 #endif
280 #ifdef TEST_T11
281 test_container(deque<test_type, n_alloc_type>(), true);
282 #endif
283 #ifdef TEST_T12
284 test_container(deque<test_type, so_alloc_type>(), true);
285 #endif
286 #ifdef TEST_T13
287 test_container(deque<test_type, bit_alloc_type>(), true);
288 #endif
289 #ifdef TEST_T14
290 test_container(deque<test_type, po_alloc_type>(), true);
291 #endif
293 #ifdef TEST_T15
294 test_container(map<test_type, test_type, compare_type,
295 m_pair_alloc_type>(), true);
296 #endif
297 #ifdef TEST_T16
298 test_container(map<test_type, test_type, compare_type,
299 n_pair_alloc_type>(), true);
300 #endif
301 #ifdef TEST_T17
302 test_container(map<test_type, test_type, compare_type,
303 so_pair_alloc_type>(), true);
304 #endif
305 #ifdef TEST_T18
306 test_container(map<test_type, test_type, compare_type,
307 bit_pair_alloc_type>(), true);
308 #endif
309 #ifdef TEST_T19
310 test_container(map<test_type, test_type, compare_type,
311 po_pair_alloc_type>(), true);
312 #endif
314 #ifdef TEST_T20
315 test_container(set<test_type, compare_type, m_alloc_type>(), true);
316 #endif
317 #ifdef TEST_T21
318 test_container(set<test_type, compare_type, n_alloc_type>(), true);
319 #endif
320 #ifdef TEST_T22
321 test_container(set<test_type, compare_type, so_alloc_type>(), true);
322 #endif
323 #ifdef TEST_T23
324 test_container(set<test_type, compare_type, bit_alloc_type>(), true);
325 #endif
326 #ifdef TEST_T24
327 test_container(set<test_type, compare_type, po_alloc_type>(), true);
328 #endif
329 return 0;