// { dg-options "-std=gnu++0x" }
-// Copyright (C) 2011 Free Software Foundation, Inc.
+// Copyright (C) 2011-2013 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
// <http://www.gnu.org/licenses/>.
#include <sstream>
+#include <tr1/unordered_set>
#include <unordered_set>
#include <testsuite_performance.h>
{
// Bench using an unordered_set<int>. Hash functor for int is quite
// predictable so it helps bench very specific use cases.
- template<bool use_cache>
- void bench()
+ template<typename _ContType>
+ void bench(const char* desc)
{
using namespace __gnu_test;
- std::ostringstream ostr;
- ostr << "unordered_set<int> " << (use_cache ? "with" : "without")
- << " cache";
- const std::string desc = ostr.str();
time_counter time;
resource_counter resource;
const int nb = 200000;
start_counters(time, resource);
- std::__unordered_set<int, std::hash<int>, std::equal_to<int>,
- std::allocator<int>, use_cache> us;
+ _ContType us;
for (int i = 0; i != nb; ++i)
- us.insert(i);
+ us.insert(i);
stop_counters(time, resource);
- ostr.str("");
+ std::ostringstream ostr;
ostr << desc << ": first insert";
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<bool use_cache>
- void bench_str()
+ template<typename _ContType>
+ void bench_str(const char* desc)
{
using namespace __gnu_test;
- std::ostringstream ostr;
- ostr << "unordered_set<string> " << (use_cache ? "with" : "without")
- << " cache";
- const std::string desc = ostr.str();
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)
start_counters(time, resource);
- std::__unordered_set<std::string, std::hash<std::string>,
- std::equal_to<std::string>,
- std::allocator<std::string>, use_cache> us;
+ _ContType us;
for (int i = 0; i != nb; ++i)
us.insert(strs[i]);
}
}
+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<false>();
- bench<true>();
- bench_str<false>();
- bench_str<true>();
+ 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;
}