1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
// { dg-options "-std=gnu++11" }
// Copyright (C) 2011-2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include <sstream>
#include <tr1/unordered_set>
#include <unordered_set>
#include <testsuite_performance.h>
namespace
{
// Bench using an unordered_set<int>. Hash functor for int is quite
// predictable so it helps bench very specific use cases.
template<typename _ContType>
void bench(const char* desc)
{
using namespace __gnu_test;
time_counter time;
resource_counter resource;
const int nb = 200000;
start_counters(time, resource);
_ContType us;
for (int i = 0; i != nb; ++i)
us.insert(i);
stop_counters(time, resource);
std::ostringstream ostr;
ostr << desc << ": first insert";
report_performance(__FILE__, ostr.str().c_str(), time, resource);
start_counters(time, resource);
// Here is the worst erase use case when hashtable implementation was
// something like vector<forward_list<>>. Erasing from the end was very
// costly because we need to return the iterator following the erased
// one, as the hashtable is getting emptier at each step there are
// more and more empty bucket to loop through to reach the end of the
// container and find out that it was in fact the last element.
for (int j = nb - 1; j >= 0; --j)
{
auto it = us.find(j);
if (it != us.end())
us.erase(it);
}
stop_counters(time, resource);
ostr.str("");
ostr << desc << ": erase from iterator";
report_performance(__FILE__, ostr.str().c_str(), time, resource);
start_counters(time, resource);
// This is a worst insertion use case for the current implementation as
// we insert an element at the beginning of the hashtable and then we
// insert starting at the end so that each time we need to seek up to the
// first bucket to find the first non-empty one.
us.insert(0);
for (int i = nb - 1; i >= 0; --i)
us.insert(i);
stop_counters(time, resource);
ostr.str("");
ostr << desc << ": second insert";
report_performance(__FILE__, ostr.str().c_str(), time, resource);
start_counters(time, resource);
for (int j = nb - 1; j >= 0; --j)
us.erase(j);
stop_counters(time, resource);
ostr.str("");
ostr << desc << ": erase from key";
report_performance(__FILE__, ostr.str().c_str(), time, resource);
}
// Bench using unordered_set<string> that show how important it is to cache
// hash code as computing string hash code is quite expensive compared to
// computing it for int.
template<typename _ContType>
void bench_str(const char* desc)
{
using namespace __gnu_test;
time_counter time;
resource_counter resource;
const int nb = 200000;
// First generate once strings that are going to be used throughout the
// bench:
std::ostringstream ostr;
std::vector<std::string> strs;
strs.reserve(nb);
for (int i = 0; i != nb; ++i)
{
ostr.str("");
ostr << "string #" << i;
strs.push_back(ostr.str());
}
start_counters(time, resource);
_ContType us;
for (int i = 0; i != nb; ++i)
us.insert(strs[i]);
stop_counters(time, resource);
ostr.str("");
ostr << desc << ": first insert";
report_performance(__FILE__, ostr.str().c_str(), time, resource);
start_counters(time, resource);
for (int j = nb - 1; j >= 0; --j)
{
auto it = us.find(strs[j]);
if (it != us.end())
us.erase(it);
}
stop_counters(time, resource);
ostr.str("");
ostr << desc << ": erase from iterator";
report_performance(__FILE__, ostr.str().c_str(), time, resource);
start_counters(time, resource);
us.insert(strs[0]);
for (int i = nb - 1; i >= 0; --i)
us.insert(strs[i]);
stop_counters(time, resource);
ostr.str("");
ostr << desc << ": second insert";
report_performance(__FILE__, ostr.str().c_str(), time, resource);
start_counters(time, resource);
for (int j = nb - 1; j >= 0; --j)
us.erase(strs[j]);
stop_counters(time, resource);
ostr.str("");
ostr << desc << ": erase from key";
report_performance(__FILE__, ostr.str().c_str(), time, resource);
}
}
template<bool cache>
using __uset =
std::__uset_hashtable<int, std::hash<int>, std::equal_to<int>,
std::allocator<int>,
std::__uset_traits<cache>>;
template<bool cache>
using __tr1_uset =
std::tr1::__unordered_set<int, std::hash<int>, std::equal_to<int>,
std::allocator<int>,
cache>;
template<bool cache>
using __str_uset =
std::__uset_hashtable<std::string, std::hash<std::string>,
std::equal_to<std::string>,
std::allocator<std::string>,
std::__uset_traits<cache>>;
template<bool cache>
using __tr1_str_uset =
std::tr1::__unordered_set<std::string, std::hash<std::string>,
std::equal_to<std::string>,
std::allocator<std::string>,
cache>;
int main()
{
bench<__tr1_uset<false>>(
"std::tr1::unordered_set<int> without hash code cached");
bench<__tr1_uset<true>>(
"std::tr1::unordered_set<int> with hash code cached");
bench<__uset<false>>(
"std::unordered_set<int> without hash code cached");
bench<__uset<true>>(
"std::unordered_set<int> with hash code cached");
bench<std::unordered_set<int>>(
"std::unordered_set<int> default cache");
bench_str<__tr1_str_uset<false>>(
"std::tr1::unordered_set<string> without hash code cached");
bench_str<__tr1_str_uset<true>>(
"std::tr1::unordered_set<string> with hash code cached");
bench_str<__str_uset<false>>(
"std::unordered_set<string> without hash code cached");
bench_str<__str_uset<true>>(
"std::unordered_set<string> with hash code cached");
bench_str<std::unordered_set<std::string>>(
"std::unordered_set<string> default cache");
return 0;
}
|