1 // Copyright (C) 2012-2019 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-do run { target c++11 } }
20 #include <testsuite_performance.h>
23 #include <tr1/unordered_set>
24 #include <unordered_set>
32 typedef std::random_device::result_type _Type
;
38 init(std::random_device
& randev
)
52 { bar
= random(); baz
= random(); meh
= random(); }
53 Foo(const Foo
&) = default;
59 { return std::size_t(bar
^ baz
^ meh
); }
62 operator==(const Foo
& other
) const
63 { return other
.bar
== bar
&& other
.baz
== baz
&& other
.meh
== meh
; }
69 std::size_t operator()(const T
& t
) const noexcept
73 const int sz
= 300000;
75 template<typename _ContType
>
77 bench(const char* container_desc
, const typename
_ContType::value_type
* foos
)
79 using namespace __gnu_test
;
84 resource_counter resource
;
85 start_counters(time
, resource
);
87 for (int i
= 0; i
!= sz
; ++i
)
90 stop_counters(time
, resource
);
91 std::ostringstream ostr
;
92 ostr
<< container_desc
<< sz
<< " insertion attempts, "
93 << s
.size() << " inserted";
94 report_performance(__FILE__
, ostr
.str().c_str(), time
, resource
);
96 // Try to insert again to check performance of collision detection
97 const int nb_loop
= 10;
98 start_counters(time
, resource
);
100 for (int j
= 0; j
!= nb_loop
; ++j
)
101 for (int i
= 0; i
!= sz
; ++i
)
104 stop_counters(time
, resource
);
106 ostr
<< container_desc
<< nb_loop
<< " times insertion of "
107 << sz
<< " elements";
108 report_performance(__FILE__
, ostr
.str().c_str(), time
, resource
);
112 using __tr1_uset
= std::tr1::__unordered_set
<Foo
, HashFunction
,
117 using __tr1_umset
= std::tr1::__unordered_multiset
<Foo
, HashFunction
,
122 using __uset
= std::__uset_hashtable
<Foo
, HashFunction
,
125 std::__uset_traits
<cache
>>;
127 using __umset
= std::__umset_hashtable
<Foo
, HashFunction
,
130 std::__umset_traits
<cache
>>;
134 std::_Hashtable
<Foo
, Foo
, std::allocator
<Foo
>,
135 std::__detail::_Identity
,
136 std::equal_to
<Foo
>, HashFunction
,
137 std::__detail::_Mask_range_hashing
,
138 std::__detail::_Default_ranged_hash
,
139 std::__detail::_Power2_rehash_policy
,
140 std::__uset_traits
<cache
>>;
144 std::_Hashtable
<Foo
, Foo
, std::allocator
<Foo
>,
145 std::__detail::_Identity
,
146 std::equal_to
<Foo
>, HashFunction
,
147 std::__detail::_Mask_range_hashing
,
148 std::__detail::_Default_ranged_hash
,
149 std::__detail::_Power2_rehash_policy
,
150 std::__umset_traits
<cache
>>;
154 using namespace __gnu_test
;
158 for (int i
= 0; i
!= sz
; ++i
)
160 bench
<std::tr1::unordered_set
<int>>(
161 "std::tr1::unordered_set<int> ", bars
);
162 bench
<std::unordered_set
<int>>(
163 "std::unordered_set<int> ", bars
);
169 std::random_device randev
;
170 for (int i
= 0; i
!= sz
; ++i
)
171 foos
[i
].init(randev
);
176 resource_counter resource
;
177 start_counters(time
, resource
);
179 bench
<__tr1_uset
<false>>(
180 "std::tr1::unordered_set without hash code cached ", foos
);
181 bench
<__tr1_uset
<true>>(
182 "std::tr1::unordered_set with hash code cached ", foos
);
183 bench
<__tr1_umset
<false>>(
184 "std::tr1::unordered_multiset without hash code cached ", foos
);
185 bench
<__tr1_umset
<true>>(
186 "std::tr1::unordered_multiset with hash code cached ", foos
);
188 stop_counters(time
, resource
);
189 report_performance(__FILE__
, "tr1 benches", time
, resource
);
191 start_counters(time
, resource
);
192 bench
<__uset
<false>>(
193 "std::unordered_set without hash code cached ", foos
);
195 "std::unordered_set with hash code cached ", foos
);
196 bench
<__umset
<false>>(
197 "std::unordered_multiset without hash code cached ", foos
);
198 bench
<__umset
<true>>(
199 "std::unordered_multiset with hash code cached ", foos
);
201 stop_counters(time
, resource
);
202 report_performance(__FILE__
, "std benches", time
, resource
);
204 start_counters(time
, resource
);
205 bench
<__uset2
<false>>(
206 "std::unordered_set2 without hash code cached ", foos
);
207 bench
<__uset2
<true>>(
208 "std::unordered_set2 with hash code cached ", foos
);
209 bench
<__umset2
<false>>(
210 "std::unordered_multiset2 without hash code cached ", foos
);
211 bench
<__umset2
<true>>(
212 "std::unordered_multiset2 with hash code cached ", foos
);
214 stop_counters(time
, resource
);
215 report_performance(__FILE__
, "std2 benches", time
, resource
);
217 bench
<std::unordered_set
<Foo
, HashFunction
>>(
218 "std::unordered_set default cache ", foos
);
219 bench
<std::unordered_multiset
<Foo
, HashFunction
>>(
220 "std::unordered_multiset default cache ", foos
);