1 // Copyright (C) 2012-2016 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-options "-std=gnu++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::__uset_traits
<cache
>>;
134 using namespace __gnu_test
;
138 for (int i
= 0; i
!= sz
; ++i
)
140 bench
<std::tr1::unordered_set
<int>>(
141 "std::tr1::unordered_set<int> ", bars
);
142 bench
<std::unordered_set
<int>>(
143 "std::unordered_set<int> ", bars
);
149 std::random_device randev
;
150 for (int i
= 0; i
!= sz
; ++i
)
151 foos
[i
].init(randev
);
156 resource_counter resource
;
157 start_counters(time
, resource
);
159 bench
<__tr1_uset
<false>>(
160 "std::tr1::unordered_set without hash code cached ", foos
);
161 bench
<__tr1_uset
<true>>(
162 "std::tr1::unordered_set with hash code cached ", foos
);
163 bench
<__tr1_umset
<false>>(
164 "std::tr1::unordered_multiset without hash code cached ", foos
);
165 bench
<__tr1_umset
<true>>(
166 "std::tr1::unordered_multiset with hash code cached ", foos
);
168 stop_counters(time
, resource
);
169 report_performance(__FILE__
, "tr1 benches", time
, resource
);
171 start_counters(time
, resource
);
172 bench
<__uset
<false>>(
173 "std::unordered_set without hash code cached ", foos
);
175 "std::unordered_set with hash code cached ", foos
);
176 bench
<__umset
<false>>(
177 "std::unordered_multiset without hash code cached ", foos
);
178 bench
<__umset
<true>>(
179 "std::unordered_multiset with hash code cached ", foos
);
181 stop_counters(time
, resource
);
182 report_performance(__FILE__
, "std benches", time
, resource
);
184 bench
<std::unordered_set
<Foo
, HashFunction
>>(
185 "std::unordered_set default cache ", foos
);
186 bench
<std::unordered_multiset
<Foo
, HashFunction
>>(
187 "std::unordered_multiset default cache ", foos
);