acinclude.m4 (GLIBCXX_ENABLE_ALLOCATOR): Add pool_allocator.
[gcc.git] / libstdc++-v3 / testsuite / performance / 20_util / allocator / map_mt_find.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 // 2004-03-11 Dhruv Matani <dhruvbird@HotPOP.com>
29
30 #include <new>
31 #include <map>
32 #include <cstdlib>
33 #include <string>
34 #include <pthread.h>
35
36 #include <typeinfo>
37 #include <sstream>
38 #include <ext/mt_allocator.h>
39 #include <ext/malloc_allocator.h>
40 #include <cxxabi.h>
41 #include <testsuite_performance.h>
42 #include <ext/bitmap_allocator.h>
43 #include <ext/pool_allocator.h>
44
45 using namespace std;
46 using __gnu_cxx::malloc_allocator;
47 using __gnu_cxx::__mt_alloc;
48 using __gnu_cxx::bitmap_allocator;
49 using __gnu_cxx::__pool_alloc;
50
51 typedef int test_type;
52
53 using namespace __gnu_cxx;
54 using namespace std;
55
56 bool less_int (int x1, int x2) { return x1<x2; }
57
58
59 #if defined USE_FUNCTION_COMPARE
60 #define COMPARE_T typeof(&less_int)
61 #define COMPARE_F &less_int
62 #else
63 #define COMPARE_T std::less<int>
64 #define COMPARE_F std::less<int>()
65 #endif
66
67 template <typename Alloc>
68 void *find_proc (void *_vptr)
69 {
70 map<int, string, COMPARE_T, Alloc> *_mptr =
71 reinterpret_cast<map<int, string, COMPARE_T, Alloc>*>(_vptr);
72
73 for (int i = 0; i < 700000; ++i)
74 {
75 int Finder = rand() % 2000000;
76 _mptr->find (Finder);
77 }
78 return _vptr;
79 }
80
81
82 template <typename Alloc>
83 int do_test ()
84 {
85 COMPARE_T _comp = COMPARE_F;
86 map<int, string, COMPARE_T, Alloc> imap (_comp);
87 int x = 2;
88 pthread_t thr[3];
89 const int Iter = 1000000;
90
91 while (x--)
92 {
93 for (int i = 0; i < 300000; ++i)
94 imap.insert (make_pair (rand()%1000000, "_test_data"));
95
96 for (int i = 0; i < 100000; ++i)
97 imap.insert (make_pair (rand()%2000000, "_test_data"));
98
99 pthread_create (&thr[0], NULL, find_proc<Alloc>, (void*)&imap);
100 pthread_create (&thr[1], NULL, find_proc<Alloc>, (void*)&imap);
101 pthread_create (&thr[2], NULL, find_proc<Alloc>, (void*)&imap);
102
103 pthread_join (thr[0], 0);
104 pthread_join (thr[1], 0);
105 pthread_join (thr[2], 0);
106
107 imap.clear ();
108 }
109 return Iter;
110 }
111
112 template <typename Alloc>
113 void exec_tests ()
114 {
115 using namespace __gnu_test;
116 int status;
117 COMPARE_T _comp = COMPARE_F;
118 map<int, string, COMPARE_T, Alloc> obj (_comp);
119
120 time_counter time;
121 resource_counter resource;
122 clear_counters(time, resource);
123 start_counters(time, resource);
124 int test_iterations = do_test<Alloc>();
125 stop_counters(time, resource);
126
127 std::ostringstream comment;
128 comment << "iterations: " << test_iterations <<endl;
129 comment << "type: " << abi::__cxa_demangle(typeid(obj).name(),
130 0, 0, &status);
131 report_header(__FILE__, comment.str());
132 report_performance(__FILE__, string(), time, resource);
133 }
134
135
136 int main()
137 {
138 #ifdef TEST_T0
139 exec_tests<new_allocator<int> >();
140 #endif
141 #ifdef TEST_T1
142 exec_tests<malloc_allocator<int> >();
143 #endif
144 #ifdef TEST_T2
145 exec_tests<bitmap_allocator<int> >();
146 #endif
147 #ifdef TEST_T3
148 exec_tests<__mt_alloc<int> >();
149 #endif
150 #ifdef TEST_T4
151 exec_tests<__pool_alloc<int> >();
152 #endif
153 }