OSDN Git Service

2003-07-24 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / performance / allocator.cc
1 // Copyright (C) 2003 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  * 
30  *
31  * The goal with this application is to compare the performance
32  * between different STL allocators relative to the default
33  * __pool_alloc.
34  *
35  * The container used for the tests is vector, which as stated by
36  * SGI "Vector is the simplest of the STL container classes, and in 
37  * many cases the most efficient.".
38  *
39  * NOTE! The vector<> container does some "caching" of it's own and
40  * therefore we redeclare the variable in each iteration (forcing the
41  * const/destr to be called and thus free memory).
42  *
43  * NOTE! The usage of gettimeofday is unwanted since it's not POSIX,
44  * however I have not found a more generic system call to use - 
45  * ideas are greatly appriciated!
46  *
47  * NOTE! This version only does operations on vector<int>. More/other
48  * data types should maybe also be tested - ideas are always welcome!
49  *
50  * I assume that glibc/underlying malloc() implementation has been
51  * compiled with -O2 thus should this application also be compiled
52  * with -O2 in order to get relevant results.
53  */
54
55 // 2003-02-05 Stefan Olsson <stefan@snon.net>
56
57 #include <vector>
58 #include <sys/time.h>
59 #include <ext/mt_allocator.h>
60 #include <ext/malloc_allocator.h>
61 #include <testsuite_performance.h>
62
63 using namespace std;
64 using __gnu_cxx::__malloc_alloc;
65 using __gnu_cxx::__mt_alloc;
66
67 /*
68  * In order to avoid that the time it takes for the application to 
69  * startup/shutdown affect the end result, we define a target 
70  * duration (in seconds) for which all tests should run.
71  * Each test is responsible for "calibrating" their # of iterations 
72  * to come as close as possible to this target based on the time
73  * it takes to complete the test using the default __pool_alloc.
74  */
75 int target_run_time = 10;
76
77 /*
78  * The number of iterations to be performed in order to figure out
79  * the "speed" of this computer and adjust the number of iterations
80  * needed to come close to target_run_time.
81  */
82 int calibrate_iterations = 100000;
83
84 /*
85  * The number of values to insert in the vector, 32 will cause 
86  * 5 (re)allocations to be performed (sizes 4, 8, 16, 32 and 64)
87  * This means that all allocations are within _MAX_BYTES = 128
88  * as defined in stl_alloc.h for __pool_alloc.
89  * Whether or not this value is relevant in "the real world" 
90  * or not I don't know and should probably be investigated in 
91  * more detail.
92  */
93 int insert_values = 32;
94
95 static struct timeval _tstart, _tend;
96 static struct timezone tz;
97
98 void
99 tstart(void)
100 {
101   gettimeofday(&_tstart, &tz);
102 }
103
104 void
105 tend(void)
106 {
107   gettimeofday(&_tend, &tz);
108 }
109
110 double
111 tval()
112 {
113   double t1, t2;
114
115   t1 =(double)_tstart.tv_sec +(double)_tstart.tv_usec/(1000*1000);
116   t2 =(double)_tend.tv_sec +(double)_tend.tv_usec/(1000*1000);
117   return t2 - t1;
118 }
119
120 int
121 calibrate_test_ints(void)
122 {
123   tstart();
124   for (int i = 0; i < calibrate_iterations; i++)
125   {
126     vector<int> v1;
127
128     for(int j = 0; j < insert_values; j++)
129       v1.push_back(1);
130   }
131   tend();
132
133   return(int)((double)target_run_time / tval()) * calibrate_iterations;
134 }
135
136 double
137 test_ints_pool_alloc(int iterations)
138 {
139   tstart();
140   for(int i = 0; i < iterations; i++)
141   {
142     vector<int> v1;
143
144     for(int j = 0; j < insert_values; j++)
145       v1.push_back(1);
146   }
147   tend();
148
149   return tval();
150 }
151
152 double
153 test_ints_malloc_alloc(int iterations)
154 {
155   tstart();
156   for(int i = 0; i < iterations; i++)
157   {
158     vector<int, __malloc_alloc<0> > v1;
159
160     for(int j = 0; j < insert_values; j++)
161     {
162       v1.push_back(1);
163     }
164   }
165   tend();
166
167   return tval();
168 }
169
170 double
171 test_ints_mt_alloc(int iterations)
172 {
173   tstart();
174   for(int i = 0; i < iterations; i++)
175   {
176     vector<int, __mt_alloc<0> > v1;
177
178     for(int j = 0; j < insert_values; j++)
179     {
180       v1.push_back(1);
181     }
182   }
183   tend();
184
185   return tval();
186 }
187
188 // http://gcc.gnu.org/ml/libstdc++/2001-05/msg00105.html
189 // http://gcc.gnu.org/ml/libstdc++/2003-05/msg00231.html
190 int main(void)
191 {
192   using namespace __gnu_test;
193
194   time_counter time;
195   resource_counter resource;
196
197   int iterations = calibrate_test_ints();
198
199   start_counters(time, resource);
200   test_ints_pool_alloc(iterations);
201   stop_counters(time, resource);
202   report_performance(__FILE__, "default", time, resource);
203   clear_counters(time, resource);
204
205   start_counters(time, resource);
206   test_ints_malloc_alloc(iterations);
207   stop_counters(time, resource);
208   report_performance(__FILE__, "malloc", time, resource);
209   clear_counters(time, resource);
210
211   start_counters(time, resource);
212   test_ints_mt_alloc(iterations);
213   stop_counters(time, resource);
214   report_performance(__FILE__, "mt", time, resource);
215
216   return 0;
217 }