OSDN Git Service

2004-01-14 Stefan Olsson <stefan@snon.net>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / performance / allocator.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 <typeinfo>
39 #include <sstream>
40 #include <ext/mt_allocator.h>
41 #include <ext/malloc_allocator.h>
42 #include <cxxabi.h>
43 #include <testsuite_performance.h>
44
45 using namespace std;
46 using __gnu_cxx::malloc_allocator;
47 using __gnu_cxx::__mt_alloc;
48
49 typedef int test_type;
50
51 // The number of iterations to be performed.
52 int iterations;
53
54 // The number of values to insert in the container, 32 will cause 5
55 // (re)allocations to be performed (sizes 4, 8, 16, 32 and 64)
56 // This means that all allocations are within _MAX_BYTES = 128 as
57 // defined in stl_alloc.h for __pool_alloc.  Whether or not this
58 // value is relevant in "the real world" or not I don't know and
59 // should probably be investigated in more detail.
60 int insert_values = 128;
61
62 template<typename Container>
63   int
64   do_loop()
65   {
66     int test_iterations = 0;
67     try
68       {
69         Container obj;
70         while (test_iterations < iterations)
71           {
72             for (int j = 0; j < insert_values; ++j)
73               obj.push_back(test_iterations);
74             ++test_iterations;
75           }
76       }
77     catch(...)
78       {
79         // No point allocating all available memory, repeatedly.        
80       }
81     return test_iterations;
82   }
83
84 template<typename Container>
85   void
86   calibrate_iterations()
87   {
88     int try_iterations = iterations = 100000;
89     int test_iterations;
90
91     __gnu_test::time_counter timer;
92     timer.start();
93     test_iterations = do_loop<Container>();
94     timer.stop();
95
96     if (try_iterations > test_iterations && test_iterations > iterations)
97       iterations = test_iterations - 100;
98     else
99       {
100         double tics = timer.real_time();
101         double iterpc = test_iterations / tics; //iterations per clock
102         double xtics = 200; // works for linux 2gig x86
103         iterations = static_cast<int>(xtics * iterpc);
104       }
105   }
106
107 template<typename Container>
108   void
109   test_container(Container obj)
110   {
111     using namespace __gnu_test;
112     int status;
113
114     time_counter time;
115     resource_counter resource;
116     clear_counters(time, resource);
117     start_counters(time, resource);
118     int test_iterations = do_loop<Container>();
119     stop_counters(time, resource);
120  
121     std::ostringstream comment;
122     comment << "iterations: " << test_iterations << '\t';
123     comment << "type: " << abi::__cxa_demangle(typeid(obj).name(),
124                                                0, 0, &status);
125     report_header(__FILE__, comment.str());
126     report_performance(__FILE__, string(), time, resource);
127   }
128
129 // http://gcc.gnu.org/ml/libstdc++/2001-05/msg00105.html
130 // http://gcc.gnu.org/ml/libstdc++/2003-05/msg00231.html
131 int main(void)
132 {
133   calibrate_iterations<vector<test_type> >();
134   test_container(vector<test_type>());
135   test_container(vector<test_type, malloc_allocator<test_type> >());
136   test_container(vector<test_type, __mt_alloc<test_type> >());
137
138   calibrate_iterations<list<test_type> >();
139   test_container(list<test_type>());
140   test_container(list<test_type, malloc_allocator<test_type> >());
141   test_container(list<test_type, __mt_alloc<test_type> >());
142
143   return 0;
144 }