1 // { dg-options "-std=gnu++0x" }
3 // Copyright (C) 2011 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
22 # include <tr1/unordered_set>
24 # include <unordered_set>
26 #include <testsuite_performance.h>
30 // Bench using an unordered_set<int>. Hash functor for int is quite
31 // predictable so it helps bench very specific use cases.
32 template<bool use_cache
>
35 using namespace __gnu_test
;
36 std::ostringstream ostr
;
37 ostr
<< "unordered_set<int> " << (use_cache
? "with" : "without")
39 const std::string desc
= ostr
.str();
42 resource_counter resource
;
44 const int nb
= 200000;
45 start_counters(time
, resource
);
48 std::tr1::__unordered_set
<int, std::hash
<int>, std::equal_to
<int>,
52 std::__uset_hashtable
<int, std::hash
<int>, std::equal_to
<int>,
54 std::__uset_traits
<use_cache
>> us
;
56 for (int i
= 0; i
!= nb
; ++i
)
59 stop_counters(time
, resource
);
61 ostr
<< desc
<< ": first insert";
62 report_performance(__FILE__
, ostr
.str().c_str(), time
, resource
);
64 start_counters(time
, resource
);
66 // Here is the worst erase use case when hashtable implementation was
67 // something like vector<forward_list<>>. Erasing from the end was very
68 // costly because we need to return the iterator following the erased
69 // one, as the hashtable is getting emptier at each step there are
70 // more and more empty bucket to loop through to reach the end of the
71 // container and find out that it was in fact the last element.
72 for (int j
= nb
- 1; j
>= 0; --j
)
79 stop_counters(time
, resource
);
81 ostr
<< desc
<< ": erase from iterator";
82 report_performance(__FILE__
, ostr
.str().c_str(), time
, resource
);
84 start_counters(time
, resource
);
86 // This is a worst insertion use case for the current implementation as
87 // we insert an element at the begining of the hashtable and then we
88 // insert starting at the end so that each time we need to seek up to the
89 // first bucket to find the first non-empty one.
91 for (int i
= nb
- 1; i
>= 0; --i
)
94 stop_counters(time
, resource
);
96 ostr
<< desc
<< ": second insert";
97 report_performance(__FILE__
, ostr
.str().c_str(), time
, resource
);
99 start_counters(time
, resource
);
101 for (int j
= nb
- 1; j
>= 0; --j
)
104 stop_counters(time
, resource
);
106 ostr
<< desc
<< ": erase from key";
107 report_performance(__FILE__
, ostr
.str().c_str(), time
, resource
);
110 // Bench using unordered_set<string> that show how important it is to cache
111 // hash code as computing string hash code is quite expensive compared to
112 // computing it for int.
113 template<bool use_cache
>
116 using namespace __gnu_test
;
117 std::ostringstream ostr
;
118 ostr
<< "unordered_set<string> " << (use_cache
? "with" : "without")
120 const std::string desc
= ostr
.str();
123 resource_counter resource
;
125 const int nb
= 200000;
126 // First generate once strings that are going to be used throughout the
128 std::vector
<std::string
> strs
;
130 for (int i
= 0; i
!= nb
; ++i
)
133 ostr
<< "string #" << i
;
134 strs
.push_back(ostr
.str());
137 start_counters(time
, resource
);
140 std::tr1::__unordered_set
<std::string
, std::hash
<std::string
>,
141 std::equal_to
<std::string
>,
142 std::allocator
<std::string
>,
145 std::__uset_hashtable
<std::string
, std::hash
<std::string
>,
146 std::equal_to
<std::string
>,
147 std::allocator
<std::string
>,
148 std::__uset_traits
<use_cache
>> us
;
150 for (int i
= 0; i
!= nb
; ++i
)
153 stop_counters(time
, resource
);
155 ostr
<< desc
<< ": first insert";
156 report_performance(__FILE__
, ostr
.str().c_str(), time
, resource
);
158 start_counters(time
, resource
);
160 for (int j
= nb
- 1; j
>= 0; --j
)
162 auto it
= us
.find(strs
[j
]);
167 stop_counters(time
, resource
);
169 ostr
<< desc
<< ": erase from iterator";
170 report_performance(__FILE__
, ostr
.str().c_str(), time
, resource
);
172 start_counters(time
, resource
);
175 for (int i
= nb
- 1; i
>= 0; --i
)
178 stop_counters(time
, resource
);
180 ostr
<< desc
<< ": second insert";
181 report_performance(__FILE__
, ostr
.str().c_str(), time
, resource
);
183 start_counters(time
, resource
);
185 for (int j
= nb
- 1; j
>= 0; --j
)
188 stop_counters(time
, resource
);
190 ostr
<< desc
<< ": erase from key";
191 report_performance(__FILE__
, ostr
.str().c_str(), time
, resource
);