1 // Copyright (C) 2003 Free Software Foundation, Inc.
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)
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,
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.
31 * The goal with this application is to compare the performance
32 * between different STL allocators relative to the default
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.".
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).
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!
47 * NOTE! This version only does operations on vector<int>. More/other
48 * data types should maybe also be tested - ideas are always welcome!
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.
55 // 2003-02-05 Stefan Olsson <stefan@snon.net>
59 #include <ext/mt_allocator.h>
60 #include <ext/malloc_allocator.h>
61 #include <testsuite_performance.h>
64 using __gnu_cxx::__malloc_alloc;
65 using __gnu_cxx::__mt_alloc;
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.
75 int target_run_time = 10;
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.
82 int calibrate_iterations = 100000;
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
93 int insert_values = 32;
95 static struct timeval _tstart, _tend;
96 static struct timezone tz;
101 gettimeofday(&_tstart, &tz);
107 gettimeofday(&_tend, &tz);
115 t1 =(double)_tstart.tv_sec +(double)_tstart.tv_usec/(1000*1000);
116 t2 =(double)_tend.tv_sec +(double)_tend.tv_usec/(1000*1000);
121 calibrate_test_ints(void)
124 for (int i = 0; i < calibrate_iterations; i++)
128 for(int j = 0; j < insert_values; j++)
133 return(int)((double)target_run_time / tval()) * calibrate_iterations;
137 test_ints_pool_alloc(int iterations)
140 for(int i = 0; i < iterations; i++)
144 for(int j = 0; j < insert_values; j++)
153 test_ints_malloc_alloc(int iterations)
156 for(int i = 0; i < iterations; i++)
158 vector<int, __malloc_alloc<0> > v1;
160 for(int j = 0; j < insert_values; j++)
171 test_ints_mt_alloc(int iterations)
174 for(int i = 0; i < iterations; i++)
176 vector<int, __mt_alloc<0> > v1;
178 for(int j = 0; j < insert_values; j++)
188 // http://gcc.gnu.org/ml/libstdc++/2001-05/msg00105.html
189 // http://gcc.gnu.org/ml/libstdc++/2003-05/msg00231.html
192 using namespace __gnu_test;
195 resource_counter resource;
197 int iterations = calibrate_test_ints();
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);
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);
211 start_counters(time, resource);
212 test_ints_mt_alloc(iterations);
213 stop_counters(time, resource);
214 report_performance(__FILE__, "mt", time, resource);