OSDN Git Service

2004-03-22 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / performance / 20_util / allocator / insert.cc
1 // Copyright (C) 2003, 2004 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.
8
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.
13
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.
18
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.
27
28 /*
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.
32  */
33
34 // 2003-02-05 Stefan Olsson <stefan@snon.net>
35
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>
50
51 using namespace std;
52
53 typedef int test_type;
54
55 // The number of iterations to be performed.
56 int iterations = 10000;
57
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;
65
66 template<typename TestType>
67   struct value_type : public pair<TestType, TestType>
68   {
69     value_type() : pair<TestType, TestType>(0, 0) { }
70
71     inline value_type operator++() { return ++this->first, *this; }
72     inline operator TestType() const { return this->first; }
73   };
74
75 template<typename Container>
76   void
77   do_loop()
78   {
79     Container obj;
80     int test_iterations = 0;
81     value_type<test_type> test_value;
82     while (test_iterations < iterations)
83       {
84         for (int j = 0; j < insert_values; ++j)
85           obj.insert(obj.end(), ++test_value);
86         ++test_iterations;
87       }
88   }
89
90 template<typename Container>
91   void*
92   do_test(void* p = NULL)
93   {
94     do_loop<Container>();
95   }
96
97 template<typename Container>
98   void
99   test_container(Container obj, bool run_threaded = false)
100   {
101     using namespace __gnu_test;
102     int status;
103
104     time_counter time;
105     resource_counter resource;
106     clear_counters(time, resource);
107     start_counters(time, resource);
108
109     if (! run_threaded)
110       {
111         do_loop<Container>();
112       }
113     else
114       {
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);
121
122         pthread_join(t1, NULL);
123         pthread_join(t2, NULL);
124         pthread_join(t3, NULL);
125         pthread_join(t4, NULL);
126 #else
127         return;
128 #endif
129       }
130
131     stop_counters(time, resource);
132  
133     std::ostringstream comment;
134     if (run_threaded)
135       comment << "4-way threaded iterations: " << iterations*4 << '\t';
136     else
137       comment << "iterations: " << iterations << '\t';
138     comment << "type: " << abi::__cxa_demangle(typeid(obj).name(),
139                                                0, 0, &status);
140     report_header(__FILE__, comment.str());
141     report_performance(__FILE__, string(), time, resource);
142   }
143
144 // http://gcc.gnu.org/ml/libstdc++/2001-05/msg00105.html
145 // http://gcc.gnu.org/ml/libstdc++/2003-05/msg00231.html
146 int main(void)
147 {
148   typedef __gnu_cxx::malloc_allocator<test_type> m_alloc_type;
149   typedef __gnu_cxx::new_allocator<test_type> n_alloc_type;
150   typedef __gnu_cxx::__mt_alloc<test_type> so_alloc_type;
151   typedef __gnu_cxx::bitmap_allocator<test_type> bit_alloc_type;
152   typedef __gnu_cxx::__pool_alloc<test_type> po_alloc_type;
153
154 #ifdef TEST_B0
155   test_container(vector<test_type, m_alloc_type>());
156 #endif
157 #ifdef TEST_B1
158   test_container(vector<test_type, n_alloc_type>());
159 #endif
160 #ifdef TEST_B2
161   test_container(vector<test_type, so_alloc_type>());
162 #endif
163 #ifdef TEST_B3
164   test_container(vector<test_type, bit_alloc_type>());
165 #endif
166 #ifdef TEST_B4
167   test_container(vector<test_type, po_alloc_type>());
168 #endif
169
170 #ifdef TEST_B5
171   test_container(list<test_type, m_alloc_type>());
172 #endif
173 #ifdef TEST_B6
174   test_container(list<test_type, n_alloc_type>());
175 #endif
176 #ifdef TEST_B7
177   test_container(list<test_type, so_alloc_type>());
178 #endif
179 #ifdef TEST_B8
180   test_container(list<test_type, bit_alloc_type>());
181 #endif
182 #ifdef TEST_B9
183   test_container(list<test_type, po_alloc_type>());
184 #endif
185
186 #ifdef TEST_B10
187   test_container(deque<test_type, m_alloc_type>());
188 #endif
189 #ifdef TEST_B11
190   test_container(deque<test_type, n_alloc_type>());
191 #endif
192 #ifdef TEST_B12
193   test_container(deque<test_type, so_alloc_type>());
194 #endif
195 #ifdef TEST_B13
196   test_container(deque<test_type, bit_alloc_type>());
197 #endif
198 #ifdef TEST_B14
199   test_container(deque<test_type, po_alloc_type>());
200 #endif
201
202   typedef less<test_type> compare_type;
203 #ifdef TEST_B15
204   test_container(map<test_type, test_type, compare_type, m_alloc_type>());
205 #endif
206 #ifdef TEST_B16
207   test_container(map<test_type, test_type, compare_type, n_alloc_type>());
208 #endif
209 #ifdef TEST_B17
210   test_container(map<test_type, test_type, compare_type, so_alloc_type>());
211 #endif
212 #ifdef TEST_B18
213   test_container(map<test_type, test_type, compare_type, bit_alloc_type>());
214 #endif
215 #ifdef TEST_B19
216   test_container(map<test_type, test_type, compare_type, po_alloc_type>());
217 #endif
218
219 #ifdef TEST_B20
220   test_container(set<test_type, compare_type, m_alloc_type>());
221 #endif
222 #ifdef TEST_B21
223   test_container(set<test_type, compare_type, n_alloc_type>());
224 #endif
225 #ifdef TEST_B22
226   test_container(set<test_type, compare_type, so_alloc_type>());
227 #endif
228 #ifdef TEST_B23
229   test_container(set<test_type, compare_type, bit_alloc_type>());
230 #endif
231 #ifdef TEST_B24
232   test_container(set<test_type, compare_type, po_alloc_type>());
233 #endif
234
235 #ifdef TEST_T0
236   test_container(vector<test_type, m_alloc_type>(), true);
237 #endif
238 #ifdef TEST_T1
239   test_container(vector<test_type, n_alloc_type>(), true);
240 #endif
241 #ifdef TEST_T2
242   test_container(vector<test_type, so_alloc_type>(), true);
243 #endif
244 #ifdef TEST_T3
245   test_container(vector<test_type, bit_alloc_type>(), true);
246 #endif
247 #ifdef TEST_T4
248   test_container(vector<test_type, po_alloc_type>(), true);
249 #endif
250
251 #ifdef TEST_T5
252   test_container(list<test_type, m_alloc_type>(), true);
253 #endif
254 #ifdef TEST_T6
255   test_container(list<test_type, n_alloc_type>(), true);
256 #endif
257 #ifdef TEST_T7
258   test_container(list<test_type, so_alloc_type>(), true);
259 #endif
260 #ifdef TEST_T8
261   test_container(list<test_type, bit_alloc_type>(), true);
262 #endif
263 #ifdef TEST_T9
264   test_container(list<test_type, po_alloc_type>(), true);
265 #endif
266
267 #ifdef TEST_T10
268   test_container(deque<test_type, m_alloc_type>(), true);
269 #endif
270 #ifdef TEST_T11
271   test_container(deque<test_type, n_alloc_type>(), true);
272 #endif
273 #ifdef TEST_T12
274   test_container(deque<test_type, so_alloc_type>(), true);
275 #endif
276 #ifdef TEST_T13
277   test_container(deque<test_type, bit_alloc_type>(), true);
278 #endif
279 #ifdef TEST_T14
280   test_container(deque<test_type, po_alloc_type>(), true);
281 #endif
282
283   typedef less<test_type> compare_type;
284 #ifdef TEST_T15
285   test_container(map<test_type, test_type, compare_type, m_alloc_type>(), true);
286 #endif
287 #ifdef TEST_T16
288   test_container(map<test_type, test_type, compare_type, n_alloc_type>(), true);
289 #endif
290 #ifdef TEST_T17
291   test_container(map<test_type, test_type, compare_type, so_alloc_type>(), true);
292 #endif
293 #ifdef TEST_T18
294   test_container(map<test_type, test_type, compare_type, bit_alloc_type>(), true);
295 #endif
296 #ifdef TEST_T19
297   test_container(map<test_type, test_type, compare_type, po_alloc_type>(), true);
298 #endif
299
300 #ifdef TEST_T20
301   test_container(set<test_type, compare_type, m_alloc_type>(), true);
302 #endif
303 #ifdef TEST_T21
304   test_container(set<test_type, compare_type, n_alloc_type>(), true);
305 #endif
306 #ifdef TEST_T22
307   test_container(set<test_type, compare_type, so_alloc_type>(), true);
308 #endif
309 #ifdef TEST_T23
310   test_container(set<test_type, compare_type, bit_alloc_type>(), true);
311 #endif
312 #ifdef TEST_T24
313   test_container(set<test_type, compare_type, po_alloc_type>(), true);
314 #endif
315   return 0;
316 }