Debian Codegraph @

libstdc++-v3/testsuite/performance/23_containers/insert/unordered_set_range_insert.cc

raw
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 const int nb_copies = 2;
15
16 // Perfect std::string hasher knowing how string instances have been
17 // generated. It is not tag as slow so that hash code is not cached.
18 // It is easier to show impact of hint in this context.
19 struct hasher
20 {
21 static int nb_calls;
22
23 std::size_t
24 operator()(const std::string& str) const noexcept
25 {
26 ++nb_calls;
27 std::hash<std::string> std_hasher;
28 auto hash = std_hasher(pattern);
29 std::istringstream isstr(str.substr(pattern.length()));
30 int idx = -1;
31 isstr >> idx;
32 if (idx != -1)
33 return (std::size_t)(hash / sz) * sz + idx;
34
35 return hash;
36 }
37 };
38
39 // Like previous hasher but tagged as slow.
40 struct slow_hasher
41 {
42 std::size_t
43 operator()(const std::string& str) const noexcept
44 { return hasher{}(str); }
45 };
46
47 int hasher::nb_calls = 0;
48
49 template<typename _Hash>
50 using us_t = std::unordered_set<std::string, _Hash>;
51
52 template<typename _Hash>
53 void
54 insert_once_individually(const std::vector<std::string>& strs,
55 us_t<_Hash>& s)
56 {
57 for (std::size_t i = 0; i != strs.size(); ++i)
58 s.insert(strs[i]);
59 }
60
61 template<typename _Hash>
62 void
63 insert_once_range(const std::vector<std::string>& strs,
64 us_t<_Hash>& s)
65 {
66 s.insert("initial string to not leave s empty");
67 s.insert(strs.begin(), strs.end());
68 }
69
70 template<typename _Hash>
71 void
72 insert_individually(const std::vector<std::string>& strs,
73 us_t<_Hash>& s)
74 {
75 for (int j = 1; j != nb_copies; ++j)
76 for (std::size_t i = 0; i != strs.size(); ++i)
77 s.insert(strs[i]);
78 }
79
80 template<typename _Hash>
81 void
82 insert_range(const std::vector<std::string>& strs,
83 us_t<_Hash>& s)
84 {
85 s.insert("initial string to not leave s empty");
86 for (int j = 1; j != nb_copies; ++j)
87 s.insert(strs.begin(), strs.end());
88 }
89 }
90
91 template<typename _Hash>
92 void bench(const char* ctx)
93 {
94 using namespace __gnu_test;
95
96 const int nb_iter = 10;
97
98 std::vector<std::string> strs;
99 strs.reserve(sz / nb_copies);
100
101 for (int i = 0; i != sz / nb_copies; ++i)
102 {
103 std::ostringstream osstr;
104 osstr << pattern << i;
105 strs.push_back(osstr.str());
106 }
107
108 us_t<_Hash> s;
109 s.reserve(sz / nb_copies);
110
111 // Warm up.
112 {
113 for (auto str : strs)
114 for (int j = 0; j != nb_copies; ++j)
115 s.insert(str);
116 }
117
118 int nb_calls_once_individually = 0;
119 int nb_calls_once_range = 0;
120 int nb_calls_individually = 0;
121 int nb_calls_range = 0;
122 time_counter time_once_individually, time_once_range;
123 time_counter time_individually, time_range;
124 resource_counter resource_once_individually, resource_once_range;
125 resource_counter resource_individually, resource_range;
126
127 for (int i = 0; i != nb_iter; ++i)
128 {
129 {
130 hasher::nb_calls = 0;
131 s.clear();
132 start_counters(time_once_individually, resource_once_individually);
133 insert_once_individually(strs, s);
134 stop_counters(time_once_individually, resource_once_individually);
135 nb_calls_once_individually += hasher::nb_calls;
136 }
137
138 {
139 hasher::nb_calls = 0;
140 s.clear();
141 start_counters(time_once_range, resource_once_range);
142 insert_once_range(strs, s);
143 stop_counters(time_once_range, resource_once_range);
144 nb_calls_once_range += hasher::nb_calls;
145 }
146
147 {
148 hasher::nb_calls = 0;
149 s.clear();
150 start_counters(time_individually, resource_individually);
151 insert_individually(strs, s);
152 stop_counters(time_individually, resource_individually);
153 nb_calls_individually += hasher::nb_calls;
154 }
155
156 {
157 hasher::nb_calls = 0;
158 s.clear();
159 start_counters(time_range, resource_range);
160 insert_range(strs, s);
161 stop_counters(time_range, resource_range);
162 nb_calls_range += hasher::nb_calls;
163 }
164 }
165
166 std::ostringstream ostr;
167 ostr << ctx << ' '
168 << nb_copies << " X " << sz / nb_copies << " inserts individually";
169 if (nb_calls_individually != 0)
170 ostr << ' ' << nb_calls_individually << " calls";
171 report_performance(__FILE__, ostr.str().c_str(),
172 time_individually, resource_individually);
173
174 ostr.str("");
175 ostr << ctx << ' '
176 << nb_copies << " X " << sz / nb_copies << " inserts in range";
177 if (nb_calls_range)
178 ostr << ' ' << nb_calls_range << " calls";
179 report_performance(__FILE__, ostr.str().c_str(),
180 time_range, resource_range);
181
182 ostr.str("");
183 ostr << ctx << ' '
184 << sz / nb_copies << " X inserts individually";
185 if (nb_calls_once_individually != 0)
186 ostr << ' ' << nb_calls_once_individually << " calls";
187 report_performance(__FILE__, ostr.str().c_str(),
188 time_once_individually, resource_once_individually);
189
190 ostr.str("");
191 ostr << ctx << ' '
192 << sz / nb_copies << " X inserts in range";
193 if (nb_calls_once_range != 0)
194 ostr << ' ' << nb_calls_once_range << " calls";
195 report_performance(__FILE__, ostr.str().c_str(),
196 time_once_range, resource_once_range);
197 }
198
199 namespace std
200 {
201 template<>
202 struct __is_fast_hash<slow_hasher> : std::false_type
203 { };
204 }
205
206 int main()
207 {
208 bench<hasher>("hash code NOT cached");
209 bench<slow_hasher>("hash code cached");
210 return 0;
211 }