| 1 | // { dg-do run { target c++11 } } |
| 2 | |
| 3 | #include <testsuite_performance.h> |
| 4 | |
| 5 | #include <sstream> |
| 6 | #include <string> |
| 7 | #include <vector> |
| 8 | #include <unordered_set> |
| 9 | |
| 10 | namespace |
| 11 | { |
| 12 | const int sz = 2000000; |
| 13 | const std::string pattern = "long enough test string #"; |
| 14 | |
| 15 | // Perfect std::string hasher knowing how string instances have been |
| 16 | // generated. It is not tag as slow so that hash code is not cached. |
| 17 | // It is easier to show impact of hint in this context. |
| 18 | struct hasher |
| 19 | { |
| 20 | std::size_t |
| 21 | operator()(const std::string& str) const noexcept |
| 22 | { |
| 23 | std::hash<std::string> std_hasher; |
| 24 | auto hash = std_hasher(pattern); |
| 25 | std::istringstream isstr(str.substr(pattern.length())); |
| 26 | int idx; |
| 27 | isstr >> idx; |
| 28 | return (std::size_t)(hash / sz) * sz + idx; |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | // Like previous hasher but tagged as slow. |
| 33 | struct slow_hasher |
| 34 | { |
| 35 | std::size_t |
| 36 | operator()(const std::string& str) const noexcept |
| 37 | { return hasher{}(str); } |
| 38 | }; |
| 39 | |
| 40 | template<typename _Hash> |
| 41 | using us_t = std::unordered_set<std::string, _Hash>; |
| 42 | |
| 43 | template<typename _Hash> |
| 44 | void |
| 45 | insert_with_perfect_hint(const std::vector<std::string>& strs, |
| 46 | us_t<_Hash>& s) |
| 47 | { |
| 48 | auto hint = s.end(); |
| 49 | for (auto str : strs) |
| 50 | { |
| 51 | auto insert_pos = s.insert(hint, str); |
| 52 | if (std::next(insert_pos) == s.end()) |
| 53 | hint = insert_pos; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | template<typename _Hash> |
| 58 | void |
| 59 | insert_with_bad_hint(const std::vector<std::string>& strs, |
| 60 | us_t<_Hash>& s) |
| 61 | { |
| 62 | auto hint = s.begin(); |
| 63 | for (auto str : strs) |
| 64 | { |
| 65 | auto insert_pos = s.insert(hint, str); |
| 66 | if (std::next(insert_pos) == hint) |
| 67 | hint = s.begin(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | template<typename _Hash> |
| 72 | void |
| 73 | insert_without_hint(const std::vector<std::string>& strs, |
| 74 | us_t<_Hash>& s) |
| 75 | { |
| 76 | for (auto str : strs) |
| 77 | s.insert(str); |
| 78 | } |
| 79 | |
| 80 | template<typename _Hash> |
| 81 | void |
| 82 | insert_range(const std::vector<std::string>& strs, |
| 83 | us_t<_Hash>& s) |
| 84 | { s.insert(strs.begin(), strs.end()); } |
| 85 | } |
| 86 | |
| 87 | template<typename _Hash> |
| 88 | void bench(const char* ctx) |
| 89 | { |
| 90 | using namespace __gnu_test; |
| 91 | |
| 92 | const int nb_iter = 10; |
| 93 | |
| 94 | std::vector<std::string> strs; |
| 95 | strs.reserve(sz); |
| 96 | |
| 97 | for (int i = 0; i != sz; ++i) |
| 98 | { |
| 99 | std::ostringstream osstr; |
| 100 | osstr << pattern << i; |
| 101 | strs.push_back(osstr.str()); |
| 102 | } |
| 103 | |
| 104 | us_t<_Hash> s; |
| 105 | s.reserve(sz); |
| 106 | |
| 107 | // Warm up. |
| 108 | { |
| 109 | for (auto str : strs) |
| 110 | s.insert(str); |
| 111 | } |
| 112 | |
| 113 | time_counter time_no_hint, time_bad_hint, time_perfect_hint, |
| 114 | time_range; |
| 115 | resource_counter resource_no_hint, resource_bad_hint, resource_perfect_hint, |
| 116 | resource_range; |
| 117 | |
| 118 | for (int i = 0; i != nb_iter; ++i) |
| 119 | { |
| 120 | // Bad hint |
| 121 | { |
| 122 | s.clear(); |
| 123 | start_counters(time_bad_hint, resource_bad_hint); |
| 124 | insert_with_bad_hint(strs, s); |
| 125 | stop_counters(time_bad_hint, resource_bad_hint); |
| 126 | } |
| 127 | |
| 128 | // No hint |
| 129 | { |
| 130 | s.clear(); |
| 131 | start_counters(time_no_hint, resource_no_hint); |
| 132 | insert_without_hint(strs, s); |
| 133 | stop_counters(time_no_hint, resource_no_hint); |
| 134 | } |
| 135 | |
| 136 | // Perfect hint |
| 137 | { |
| 138 | s.clear(); |
| 139 | start_counters(time_perfect_hint, resource_perfect_hint); |
| 140 | insert_with_perfect_hint(strs, s); |
| 141 | stop_counters(time_perfect_hint, resource_perfect_hint); |
| 142 | } |
| 143 | |
| 144 | // Range insert |
| 145 | { |
| 146 | s.clear(); |
| 147 | start_counters(time_range, resource_range); |
| 148 | insert_range(strs, s); |
| 149 | stop_counters(time_range, resource_range); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | std::ostringstream ostr; |
| 154 | ostr << ctx << ' ' << sz << " insertions w/o hint"; |
| 155 | report_performance(__FILE__, ostr.str().c_str(), |
| 156 | time_no_hint, resource_no_hint); |
| 157 | |
| 158 | ostr.str(""); |
| 159 | ostr << ctx << ' ' << sz << " insertions with perfect hint"; |
| 160 | report_performance(__FILE__, ostr.str().c_str(), |
| 161 | time_perfect_hint, resource_perfect_hint); |
| 162 | |
| 163 | ostr.str(""); |
| 164 | ostr << ctx << ' ' << sz << " insertions with bad hint"; |
| 165 | report_performance(__FILE__, ostr.str().c_str(), |
| 166 | time_bad_hint, resource_bad_hint); |
| 167 | |
| 168 | ostr.str(""); |
| 169 | ostr << ctx << ' ' << sz << " range insertions"; |
| 170 | report_performance(__FILE__, ostr.str().c_str(), |
| 171 | time_range, resource_range); |
| 172 | } |
| 173 | |
| 174 | namespace std |
| 175 | { |
| 176 | template<> |
| 177 | struct __is_fast_hash<slow_hasher> : std::false_type |
| 178 | { }; |
| 179 | } |
| 180 | |
| 181 | int main() |
| 182 | { |
| 183 | bench<hasher>("hash code NOT cached"); |
| 184 | bench<slow_hasher>("hash code cached"); |
| 185 | return 0; |
| 186 | } |