]>
Commit | Line | Data |
---|---|---|
e3ef832a FD |
1 | // { dg-do run { target c++11 } } |
2 | ||
83ffe9cd | 3 | // Copyright (C) 2021-2023 Free Software Foundation, Inc. |
e3ef832a FD |
4 | // |
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) | |
9 | // any later version. | |
10 | ||
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. | |
15 | ||
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/>. | |
19 | ||
20 | #include <string> | |
21 | #include <sstream> | |
22 | #include <vector> | |
23 | #include <unordered_set> | |
24 | #include <testsuite_performance.h> | |
25 | ||
26 | namespace | |
27 | { | |
28 | const int nb_elements = 20; | |
91b334d0 | 29 | const int nb_insts = 15000; |
e3ef832a FD |
30 | |
31 | template<typename _ElemType> | |
91b334d0 | 32 | void bench(const char* desc, const std::vector<_ElemType>& elems, bool with_copy) |
e3ef832a FD |
33 | { |
34 | using namespace __gnu_test; | |
35 | ||
36 | time_counter time; | |
37 | resource_counter resource; | |
38 | ||
39 | std::vector<std::unordered_set<_ElemType>> insts(nb_insts); | |
40 | for (int j = 0; j != nb_insts; ++j) | |
41 | insts.emplace_back(); | |
42 | ||
43 | start_counters(time, resource); | |
44 | ||
45 | for (auto& us : insts) | |
46 | for (int i = 0; i != nb_elements; ++i) | |
47 | us.insert(elems[i]); | |
48 | ||
49 | stop_counters(time, resource); | |
50 | ||
51 | std::ostringstream ostr; | |
52 | ostr << desc << " 1st insert"; | |
53 | report_performance(__FILE__, ostr.str().c_str(), time, resource); | |
54 | ||
91b334d0 FD |
55 | if (with_copy) |
56 | { | |
57 | start_counters(time, resource); | |
58 | ||
59 | std::vector<std::unordered_set<_ElemType>>(insts).swap(insts); | |
60 | ||
61 | stop_counters(time, resource); | |
62 | ||
63 | ostr.str(""); | |
64 | ostr << desc << " copy"; | |
65 | report_performance(__FILE__, ostr.str().c_str(), time, resource); | |
66 | } | |
67 | ||
e3ef832a FD |
68 | start_counters(time, resource); |
69 | ||
70 | for (auto& us : insts) | |
71 | for (int i = nb_elements - 1; i >= 0; --i) | |
72 | { | |
73 | auto it = us.find(elems[i]); | |
74 | if (it != us.end()) | |
75 | us.erase(it); | |
76 | } | |
77 | ||
78 | stop_counters(time, resource); | |
79 | ||
80 | ostr.str(""); | |
81 | ostr << desc << " find/erase"; | |
82 | report_performance(__FILE__, ostr.str().c_str(), time, resource); | |
83 | ||
84 | start_counters(time, resource); | |
85 | ||
86 | for (auto& us : insts) | |
87 | { | |
88 | us.insert(elems[0]); | |
89 | for (int i = nb_elements - 1; i >= 0; --i) | |
90 | us.insert(elems[i]); | |
91 | } | |
92 | ||
93 | stop_counters(time, resource); | |
94 | ostr.str(""); | |
95 | ostr << desc << " 2nd insert"; | |
96 | report_performance(__FILE__, ostr.str().c_str(), time, resource); | |
97 | ||
98 | start_counters(time, resource); | |
99 | ||
100 | for (auto& us : insts) | |
101 | for (int j = nb_elements - 1; j >= 0; --j) | |
102 | us.erase(elems[j]); | |
103 | ||
104 | stop_counters(time, resource); | |
105 | ostr.str(""); | |
106 | ostr << desc << " erase key "; | |
107 | report_performance(__FILE__, ostr.str().c_str(), time, resource); | |
108 | } | |
109 | } | |
110 | ||
111 | int main() | |
112 | { | |
113 | { | |
114 | std::vector<int> elems; | |
115 | elems.reserve(nb_elements); | |
116 | for (int i = 0; i != nb_elements; ++i) | |
117 | elems.push_back(i); | |
118 | ||
91b334d0 FD |
119 | bench("std::unordered_set<int>: ", elems, false); |
120 | bench("std::unordered_set<int>: ", elems, true); | |
e3ef832a FD |
121 | } |
122 | ||
123 | { | |
124 | std::vector<std::string> elems; | |
125 | { | |
126 | elems.reserve(nb_elements); | |
127 | for (int i = 0; i != nb_elements; ++i) | |
128 | { | |
129 | std::ostringstream ostr; | |
130 | ostr << "string #" << i << ' ' << std::string(1000, 'a' + i); | |
131 | elems.push_back(ostr.str()); | |
132 | } | |
133 | } | |
134 | ||
91b334d0 FD |
135 | bench("std::unordered_set<string>: ", elems, false); |
136 | bench("std::unordered_set<string>: ", elems, true); | |
e3ef832a FD |
137 | } |
138 | ||
139 | return 0; | |
140 | } |