OSDN Git Service

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