OSDN Git Service

7a27c2fd3425a8b75da63916cdc04edb8ac7e79a
[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 <cxxabi.h>
47 #include <testsuite_performance.h>
48
49 using namespace std;
50
51 typedef int test_type;
52
53 // The number of iterations to be performed.
54 int iterations = 100000;
55
56 // The number of values to insert in the container, 32 will cause 5
57 // (re)allocations to be performed (sizes 4, 8, 16, 32 and 64)
58 // This means that all allocations are within _MAX_BYTES = 128 as
59 // defined in stl_alloc.h for __pool_alloc.  Whether or not this
60 // value is relevant in "the real world" or not I don't know and
61 // should probably be investigated in more detail.
62 int insert_values = 128;
63
64 template<typename TestType>
65   struct value_type : public pair<TestType, TestType>
66   {
67     value_type() : pair<TestType, TestType>(0, 0) { }
68
69     inline value_type operator++() { return ++this->first, *this; }
70     inline operator TestType() const { return this->first; }
71   };
72
73 template<typename Container>
74   int
75   do_loop(Container& obj)
76   {
77     int test_iterations = 0;
78     try
79       {
80         value_type<test_type> test_value;
81         while (test_iterations < iterations)
82           {
83             for (int j = 0; j < insert_values; ++j)
84               obj.insert(obj.end(), ++test_value);
85             ++test_iterations;
86           }
87       }
88     catch(...)
89       {
90         // No point allocating all available memory, repeatedly.        
91       }
92     return test_iterations;
93   }
94
95 template<typename Container>
96   void
97   test_container(Container obj)
98   {
99     using namespace __gnu_test;
100     int status;
101
102     time_counter time;
103     resource_counter resource;
104     clear_counters(time, resource);
105     start_counters(time, resource);
106     int test_iterations = do_loop(obj);
107     stop_counters(time, resource);
108  
109     std::ostringstream comment;
110     comment << "iterations: " << test_iterations << '\t';
111     comment << "type: " << abi::__cxa_demangle(typeid(obj).name(),
112                                                0, 0, &status);
113     report_header(__FILE__, comment.str());
114     report_performance(__FILE__, string(), time, resource);
115   }
116
117 // http://gcc.gnu.org/ml/libstdc++/2001-05/msg00105.html
118 // http://gcc.gnu.org/ml/libstdc++/2003-05/msg00231.html
119 int main(void)
120 {
121   typedef __gnu_cxx::malloc_allocator<test_type> m_alloc_type;
122   typedef __gnu_cxx::new_allocator<test_type> n_alloc_type;
123   typedef __gnu_cxx::__mt_alloc<test_type> so_alloc_type;
124
125 #ifdef TEST_B0
126   test_container(vector<test_type, m_alloc_type>());
127 #endif
128 #ifdef TEST_B1
129   test_container(vector<test_type, n_alloc_type>());
130 #endif
131 #ifdef TEST_B2
132   test_container(vector<test_type, so_alloc_type>());
133 #endif
134
135 #ifdef TEST_B3
136   test_container(list<test_type, m_alloc_type>());
137 #endif
138 #ifdef TEST_B4
139   test_container(list<test_type, n_alloc_type>());
140 #endif
141 #ifdef TEST_B5
142   test_container(list<test_type, so_alloc_type>());
143 #endif
144
145 #ifdef TEST_B6
146   test_container(deque<test_type, m_alloc_type>());
147 #endif
148 #ifdef TEST_B7
149   test_container(deque<test_type, n_alloc_type>());
150 #endif
151 #ifdef TEST_B8
152   test_container(deque<test_type, so_alloc_type>());
153 #endif
154
155   typedef less<test_type> compare_type;
156 #ifdef TEST_B9
157   test_container(map<test_type, test_type, compare_type, m_alloc_type>());
158 #endif
159 #ifdef TEST_B10
160   test_container(map<test_type, test_type, compare_type, n_alloc_type>());
161 #endif
162 #ifdef TEST_B11
163   test_container(map<test_type, test_type, compare_type, so_alloc_type>());
164 #endif
165
166 #ifdef TEST_B12
167   test_container(set<test_type, compare_type, m_alloc_type>());
168 #endif
169 #ifdef TEST_B13
170   test_container(set<test_type, compare_type, n_alloc_type>());
171 #endif
172 #ifdef TEST_B14
173   test_container(set<test_type, compare_type, so_alloc_type>());
174 #endif
175
176   return 0;
177 }