]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/performance/23_containers/insert_erase/unordered_small_size.cc
libstdc++: Optimize operations on small size hashtable [PR 68303]
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / performance / 23_containers / insert_erase / unordered_small_size.cc
CommitLineData
e3ef832a
FD
1// { dg-do run { target c++11 } }
2
3// Copyright (C) 2021 Free Software Foundation, Inc.
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
26namespace
27{
28 const int nb_elements = 20;
29 const int nb_insts = 150000;
30
31 template<typename _ElemType>
32 void bench(const char* desc, const std::vector<_ElemType>& elems)
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
55 start_counters(time, resource);
56
57 for (auto& us : insts)
58 for (int i = nb_elements - 1; i >= 0; --i)
59 {
60 auto it = us.find(elems[i]);
61 if (it != us.end())
62 us.erase(it);
63 }
64
65 stop_counters(time, resource);
66
67 ostr.str("");
68 ostr << desc << " find/erase";
69 report_performance(__FILE__, ostr.str().c_str(), time, resource);
70
71 start_counters(time, resource);
72
73 for (auto& us : insts)
74 {
75 us.insert(elems[0]);
76 for (int i = nb_elements - 1; i >= 0; --i)
77 us.insert(elems[i]);
78 }
79
80 stop_counters(time, resource);
81 ostr.str("");
82 ostr << desc << " 2nd insert";
83 report_performance(__FILE__, ostr.str().c_str(), time, resource);
84
85 start_counters(time, resource);
86
87 for (auto& us : insts)
88 for (int j = nb_elements - 1; j >= 0; --j)
89 us.erase(elems[j]);
90
91 stop_counters(time, resource);
92 ostr.str("");
93 ostr << desc << " erase key ";
94 report_performance(__FILE__, ostr.str().c_str(), time, resource);
95 }
96}
97
98int main()
99{
100 {
101 std::vector<int> elems;
102 elems.reserve(nb_elements);
103 for (int i = 0; i != nb_elements; ++i)
104 elems.push_back(i);
105
106 bench("std::unordered_set<int>: ", elems);
107 }
108
109 {
110 std::vector<std::string> elems;
111 {
112 elems.reserve(nb_elements);
113 for (int i = 0; i != nb_elements; ++i)
114 {
115 std::ostringstream ostr;
116 ostr << "string #" << i << ' ' << std::string(1000, 'a' + i);
117 elems.push_back(ostr.str());
118 }
119 }
120
121 bench("std::unordered_set<string>: ", elems);
122 }
123
124 return 0;
125}