Debian Codegraph @

libstdc++-v3/testsuite/performance/23_containers/insert/54075.cc

raw
1 // Copyright (C) 2012-2025 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 3, 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 COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do run { target c++11 } }
19
20 #include <string>
21 #include <random>
22 #include <sstream>
23 #include <tr1/unordered_set>
24 #include <unordered_set>
25
26 #include <testsuite_performance.h>
27
28 #define USE_MY_FOO 1
29
30 struct Foo
31 {
32 #if USE_MY_FOO
33
34 typedef std::random_device::result_type _Type;
35 _Type bar;
36 _Type baz;
37 _Type meh;
38
39 void
40 init(std::random_device& randev)
41 {
42 bar = randev();
43 baz = randev();
44 meh = randev();
45 }
46
47 #else
48
49 int bar;
50 int baz;
51 int meh;
52
53 Foo()
54 { bar = random(); baz = random(); meh = random(); }
55 Foo(const Foo&) = default;
56
57 #endif
58
59 std::size_t
60 hash() const noexcept
61 { return std::size_t(bar ^ baz ^ meh); }
62
63 inline bool
64 operator==(const Foo& other) const
65 { return other.bar == bar && other.baz == baz && other.meh == meh; }
66 };
67
68 struct HashFunction
69 {
70 template<typename T>
71 std::size_t operator()(const T& t) const noexcept
72 { return t.hash(); }
73 };
74
75 const int sz = 300000;
76 const int usz = sz / 2;
77
78 template<typename _ContType>
79 void
80 bench(const char* container_desc,
81 const typename _ContType::value_type* foos,
82 const typename _ContType::value_type* ufoos)
83 {
84 using namespace __gnu_test;
85
86 _ContType s;
87
88 time_counter time;
89 resource_counter resource;
90 start_counters(time, resource);
91
92 for (int i = 0; i != sz ; ++i)
93 s.insert(foos[i]);
94
95 stop_counters(time, resource);
96 std::ostringstream ostr;
97 ostr << container_desc << sz << " insertion attempts, "
98 << s.size() << " inserted";
99 report_performance(__FILE__, ostr.str().c_str(), time, resource);
100
101 // Try to insert again to check performance of collision detection
102 const int nb_loop = 10;
103 start_counters(time, resource);
104
105 for (int j = 0; j != nb_loop; ++j)
106 for (int i = 0; i != sz; ++i)
107 s.insert(foos[i]);
108
109 stop_counters(time, resource);
110 ostr.str("");
111 ostr << container_desc << nb_loop << " times insertion of "
112 << sz << " elements";
113 report_performance(__FILE__, ostr.str().c_str(), time, resource);
114
115 // Try to lookup for mostly unknown entries.
116 start_counters(time, resource);
117
118 int fcount = 0;
119 for (int j = 0; j != nb_loop; ++j)
120 for (int i = 0; i != usz; ++i)
121 fcount += s.find(ufoos[i]) != s.end() ? 1 : 0;
122
123 stop_counters(time, resource);
124 ostr.str("");
125 ostr << container_desc << nb_loop << " times lookup of "
126 << usz << " elements " << fcount / nb_loop << " found";
127 report_performance(__FILE__, ostr.str().c_str(), time, resource);
128
129 // Try again the previous operations but on a copy with potentially
130 // less memory fragmentation.
131 _ContType scopy(s);
132
133 // Try to insert again to check performance of collision detection
134 start_counters(time, resource);
135
136 for (int j = 0; j != nb_loop; ++j)
137 for (int i = 0; i != sz; ++i)
138 scopy.insert(foos[i]);
139
140 stop_counters(time, resource);
141 ostr.str("");
142 ostr << container_desc << nb_loop << " times insertion of "
143 << sz << " elements in copy";
144 report_performance(__FILE__, ostr.str().c_str(), time, resource);
145
146 // Try to lookup for mostly unknown entries.
147 start_counters(time, resource);
148
149 fcount = 0;
150 for (int j = 0; j != nb_loop; ++j)
151 for (int i = 0; i != usz; ++i)
152 fcount += scopy.find(ufoos[i]) != scopy.end() ? 1 : 0;
153
154 stop_counters(time, resource);
155 ostr.str("");
156 ostr << container_desc << nb_loop << " times lookup of "
157 << usz << " elements " << fcount / nb_loop << " found";
158 report_performance(__FILE__, ostr.str().c_str(), time, resource);
159 }
160
161 template<bool cache>
162 using __tr1_uset = std::tr1::__unordered_set<Foo, HashFunction,
163 std::equal_to<Foo>,
164 std::allocator<Foo>,
165 cache>;
166 template<bool cache>
167 using __tr1_umset = std::tr1::__unordered_multiset<Foo, HashFunction,
168 std::equal_to<Foo>,
169 std::allocator<Foo>,
170 cache>;
171 template<bool cache>
172 using __uset = std::__uset_hashtable<Foo, HashFunction,
173 std::equal_to<Foo>,
174 std::allocator<Foo>,
175 std::__uset_traits<cache>>;
176 template<bool cache>
177 using __umset = std::__umset_hashtable<Foo, HashFunction,
178 std::equal_to<Foo>,
179 std::allocator<Foo>,
180 std::__umset_traits<cache>>;
181
182 template<bool cache>
183 using __uset2 =
184 std::_Hashtable<Foo, Foo, std::allocator<Foo>,
185 std::__detail::_Identity,
186 std::equal_to<Foo>, HashFunction,
187 std::__detail::_Mask_range_hashing,
188 std::__detail::_Default_ranged_hash,
189 std::__detail::_Power2_rehash_policy,
190 std::__uset_traits<cache>>;
191
192 template<bool cache>
193 using __umset2 =
194 std::_Hashtable<Foo, Foo, std::allocator<Foo>,
195 std::__detail::_Identity,
196 std::equal_to<Foo>, HashFunction,
197 std::__detail::_Mask_range_hashing,
198 std::__detail::_Default_ranged_hash,
199 std::__detail::_Power2_rehash_policy,
200 std::__umset_traits<cache>>;
201
202 int main()
203 {
204 using namespace __gnu_test;
205
206 {
207 int bars[sz];
208 int ubars[usz];
209 for (int i = 0; i != sz; ++i)
210 bars[i] = i;
211 for (int i = 0; i != usz; ++i)
212 ubars[i] = sz + i;
213 bench<std::tr1::unordered_set<int>>(
214 "std::tr1::unordered_set<int> ", bars, ubars);
215 bench<std::unordered_set<int>>(
216 "std::unordered_set<int> ", bars, ubars);
217 }
218
219 {
220 Foo foos[sz];
221 Foo ufoos[usz];
222 #if USE_MY_FOO
223 {
224 std::random_device randev;
225 for (int i = 0; i != sz; ++i)
226 foos[i].init(randev);
227 for (int i = 0; i != usz; ++i)
228 ufoos[i].init(randev);
229 }
230 #endif
231
232 time_counter time;
233 resource_counter resource;
234 start_counters(time, resource);
235
236 bench<__tr1_uset<false>>(
237 "std::tr1::unordered_set without hash code cached ", foos, ufoos);
238 bench<__tr1_uset<true>>(
239 "std::tr1::unordered_set with hash code cached ", foos, ufoos);
240 bench<__tr1_umset<false>>(
241 "std::tr1::unordered_multiset without hash code cached ", foos, ufoos);
242 bench<__tr1_umset<true>>(
243 "std::tr1::unordered_multiset with hash code cached ", foos, ufoos);
244
245 stop_counters(time, resource);
246 report_performance(__FILE__, "tr1 benches", time, resource);
247
248 start_counters(time, resource);
249 bench<__uset<false>>(
250 "std::unordered_set without hash code cached ", foos, ufoos);
251 bench<__uset<true>>(
252 "std::unordered_set with hash code cached ", foos, ufoos);
253 bench<__umset<false>>(
254 "std::unordered_multiset without hash code cached ", foos, ufoos);
255 bench<__umset<true>>(
256 "std::unordered_multiset with hash code cached ", foos, ufoos);
257
258 stop_counters(time, resource);
259 report_performance(__FILE__, "std benches", time, resource);
260
261 start_counters(time, resource);
262 bench<__uset2<false>>(
263 "std::unordered_set2 without hash code cached ", foos, ufoos);
264 bench<__uset2<true>>(
265 "std::unordered_set2 with hash code cached ", foos, ufoos);
266 bench<__umset2<false>>(
267 "std::unordered_multiset2 without hash code cached ", foos, ufoos);
268 bench<__umset2<true>>(
269 "std::unordered_multiset2 with hash code cached ", foos, ufoos);
270
271 stop_counters(time, resource);
272 report_performance(__FILE__, "std2 benches", time, resource);
273
274 bench<std::unordered_set<Foo, HashFunction>>(
275 "std::unordered_set default cache ", foos, ufoos);
276 bench<std::unordered_multiset<Foo, HashFunction>>(
277 "std::unordered_multiset default cache ", foos, ufoos);
278 }
279 }