OSDN Git Service

b85e6e94c76934761775ceaa149adcb0b05ef6a8
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / performance / 20_util / allocator / map_thread.cc
1 // Copyright (C) 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 // libstdc++/13823 recast for this testing framework
35
36 #include <map>
37 #include <iostream>
38 #include <typeinfo>
39 #include <sstream>
40 #include <ext/mt_allocator.h>
41 #include <ext/new_allocator.h>
42 #include <ext/malloc_allocator.h>
43 #include <ext/bitmap_allocator.h>
44 #include <cxxabi.h>
45 #include <testsuite_performance.h>
46
47 using namespace std;
48 using __gnu_cxx::__mt_alloc;
49 using __gnu_cxx::new_allocator;
50 using __gnu_cxx::malloc_allocator;
51 using __gnu_cxx::bitmap_allocator;
52
53 // The number of iterations to be performed.
54 int iterations = 10000;
55
56 template<typename Container>
57   void*
58   do_loop(void* p = NULL)
59   {
60     try
61       {
62         for (int c = 0; c < 10; c++)
63           {
64             Container m;
65
66             for (unsigned i = 0; i < iterations; ++i) 
67               m[i] = i;
68           }
69       }
70     catch(...)
71       {
72         // No point allocating all available memory, repeatedly.        
73       }
74   }
75
76 template<typename Container>
77   void
78   test_container(Container obj)
79   {
80     using namespace __gnu_test;
81     int status;
82
83     time_counter time;
84     resource_counter resource;
85
86     clear_counters(time, resource);
87     start_counters(time, resource);
88     
89     pthread_t  t1, t2, t3, t4;
90     pthread_create(&t1, NULL, &do_loop<Container>, NULL);
91     pthread_create(&t2, NULL, &do_loop<Container>, NULL);
92     pthread_create(&t3, NULL, &do_loop<Container>, NULL);
93     pthread_create(&t4, NULL, &do_loop<Container>, NULL);
94
95     pthread_join(t1, NULL);
96     pthread_join(t2, NULL);
97     pthread_join(t3, NULL);
98     pthread_join(t4, NULL);
99
100     stop_counters(time, resource);
101  
102     std::ostringstream comment;
103     comment << "iterations: " << iterations << '\t';
104     comment << "type: " << abi::__cxa_demangle(typeid(obj).name(),
105                                                0, 0, &status);
106     report_header(__FILE__, comment.str());
107     report_performance(__FILE__, string(), time, resource);
108   }
109
110 int main(void)
111 {
112 #ifdef TEST_T1
113   test_container(map<int, int>());
114 #endif
115 #ifdef TEST_T2
116   test_container(map<int, int, less<const int>, new_allocator<int> >());
117 #endif
118 #ifdef TEST_T3
119   test_container(map<int, int, less<const int>, malloc_allocator<int> >());
120 #endif
121 #ifdef TEST_T4
122   test_container(map<int, int, less<const int>,
123                      __mt_alloc< pair<const int, int> > >());
124 #endif
125 #ifdef TEST_T5
126   test_container(map<int, int, less<const int>, bitmap_allocator<int> >());
127 #endif
128
129
130   return 0;
131 }