Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libstdc++-v3 / testsuite / performance / 20_util / allocator / insert_insert.cc
blobf0a3e7596a8ae90e9d0fad109011456e6993d060
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.
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 test_container(Container obj)
94 using namespace __gnu_test;
95 int status;
97 time_counter time;
98 resource_counter resource;
100 start_counters(time, resource);
101 do_loop<Container>();
102 do_loop<Container>();
103 stop_counters(time, resource);
105 std::ostringstream comment;
106 comment << "repeated iterations: " << iterations*2 << '\t';
107 comment << "type: " << abi::__cxa_demangle(typeid(obj).name(),
108 0, 0, &status);
109 report_header(__FILE__, comment.str());
110 report_performance(__FILE__, string(), time, resource);
114 // http://gcc.gnu.org/ml/libstdc++/2001-05/msg00105.html
115 // http://gcc.gnu.org/ml/libstdc++/2003-05/msg00231.html
116 int main(void)
118 typedef __gnu_cxx::malloc_allocator<test_type> m_alloc_type;
119 typedef __gnu_cxx::new_allocator<test_type> n_alloc_type;
120 typedef __gnu_cxx::__mt_alloc<test_type> so_alloc_type;
121 typedef __gnu_cxx::bitmap_allocator<test_type> bit_alloc_type;
122 typedef __gnu_cxx::__pool_alloc<test_type> po_alloc_type;
124 #ifdef TEST_S0
125 test_container(vector<test_type, m_alloc_type>());
126 #endif
127 #ifdef TEST_S1
128 test_container(vector<test_type, n_alloc_type>());
129 #endif
130 #ifdef TEST_S2
131 test_container(vector<test_type, so_alloc_type>());
132 #endif
133 #ifdef TEST_S3
134 test_container(vector<test_type, bit_alloc_type>());
135 #endif
136 #ifdef TEST_S4
137 test_container(vector<test_type, po_alloc_type>());
138 #endif
140 #ifdef TEST_S5
141 test_container(list<test_type, m_alloc_type>());
142 #endif
143 #ifdef TEST_S6
144 test_container(list<test_type, n_alloc_type>());
145 #endif
146 #ifdef TEST_S7
147 test_container(list<test_type, so_alloc_type>());
148 #endif
149 #ifdef TEST_S8
150 test_container(list<test_type, bit_alloc_type>());
151 #endif
152 #ifdef TEST_S9
153 test_container(list<test_type, po_alloc_type>());
154 #endif
156 #ifdef TEST_S10
157 test_container(deque<test_type, m_alloc_type>());
158 #endif
159 #ifdef TEST_S11
160 test_container(deque<test_type, n_alloc_type>());
161 #endif
162 #ifdef TEST_S12
163 test_container(deque<test_type, so_alloc_type>());
164 #endif
165 #ifdef TEST_S13
166 test_container(deque<test_type, bit_alloc_type>());
167 #endif
168 #ifdef TEST_S14
169 test_container(deque<test_type, po_alloc_type>());
170 #endif
172 typedef less<test_type> compare_type;
173 typedef pair<const test_type, test_type> pair_type;
174 typedef __gnu_cxx::malloc_allocator<pair_type> m_pair_alloc_type;
175 typedef __gnu_cxx::new_allocator<pair_type> n_pair_alloc_type;
176 typedef __gnu_cxx::__mt_alloc<pair_type> so_pair_alloc_type;
177 typedef __gnu_cxx::bitmap_allocator<pair_type> bit_pair_alloc_type;
178 typedef __gnu_cxx::__pool_alloc<pair_type> po_pair_alloc_type;
180 #ifdef TEST_S15
181 test_container(map<test_type, test_type, compare_type,
182 m_pair_alloc_type>());
183 #endif
184 #ifdef TEST_S16
185 test_container(map<test_type, test_type, compare_type,
186 n_pair_alloc_type>());
187 #endif
188 #ifdef TEST_S17
189 test_container(map<test_type, test_type, compare_type,
190 so_pair_alloc_type>());
191 #endif
192 #ifdef TEST_S18
193 test_container(map<test_type, test_type, compare_type,
194 bit_pair_alloc_type>());
195 #endif
196 #ifdef TEST_S19
197 test_container(map<test_type, test_type, compare_type,
198 po_pair_alloc_type>());
199 #endif
201 #ifdef TEST_S20
202 test_container(set<test_type, compare_type, m_alloc_type>());
203 #endif
204 #ifdef TEST_S21
205 test_container(set<test_type, compare_type, n_alloc_type>());
206 #endif
207 #ifdef TEST_S22
208 test_container(set<test_type, compare_type, so_alloc_type>());
209 #endif
210 #ifdef TEST_S23
211 test_container(set<test_type, compare_type, bit_alloc_type>());
212 #endif
213 #ifdef TEST_S24
214 test_container(set<test_type, compare_type, po_alloc_type>());
215 #endif
216 return 0;