1 // { dg-do run { target c++11 } }
3 // Copyright (C) 2021-2024 Free Software Foundation, Inc.
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)
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.
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/>.
23 #include <unordered_set>
24 #include <testsuite_performance.h>
28 const int nb_elements
= 20;
29 const int nb_insts
= 15000;
31 template<typename _ElemType
>
32 void bench(const char* desc
, const std::vector
<_ElemType
>& elems
, bool with_copy
)
34 using namespace __gnu_test
;
37 resource_counter resource
;
39 std::vector
<std::unordered_set
<_ElemType
>> insts(nb_insts
);
40 for (int j
= 0; j
!= nb_insts
; ++j
)
43 start_counters(time
, resource
);
45 for (auto& us
: insts
)
46 for (int i
= 0; i
!= nb_elements
; ++i
)
49 stop_counters(time
, resource
);
51 std::ostringstream ostr
;
52 ostr
<< desc
<< " 1st insert";
53 report_performance(__FILE__
, ostr
.str().c_str(), time
, resource
);
57 start_counters(time
, resource
);
59 std::vector
<std::unordered_set
<_ElemType
>>(insts
).swap(insts
);
61 stop_counters(time
, resource
);
64 ostr
<< desc
<< " copy";
65 report_performance(__FILE__
, ostr
.str().c_str(), time
, resource
);
68 start_counters(time
, resource
);
70 for (auto& us
: insts
)
71 for (int i
= nb_elements
- 1; i
>= 0; --i
)
73 auto it
= us
.find(elems
[i
]);
78 stop_counters(time
, resource
);
81 ostr
<< desc
<< " find/erase";
82 report_performance(__FILE__
, ostr
.str().c_str(), time
, resource
);
84 start_counters(time
, resource
);
86 for (auto& us
: insts
)
89 for (int i
= nb_elements
- 1; i
>= 0; --i
)
93 stop_counters(time
, resource
);
95 ostr
<< desc
<< " 2nd insert";
96 report_performance(__FILE__
, ostr
.str().c_str(), time
, resource
);
98 start_counters(time
, resource
);
100 for (auto& us
: insts
)
101 for (int j
= nb_elements
- 1; j
>= 0; --j
)
104 stop_counters(time
, resource
);
106 ostr
<< desc
<< " erase key ";
107 report_performance(__FILE__
, ostr
.str().c_str(), time
, resource
);
114 std::vector
<int> elems
;
115 elems
.reserve(nb_elements
);
116 for (int i
= 0; i
!= nb_elements
; ++i
)
119 bench("std::unordered_set<int>: ", elems
, false);
120 bench("std::unordered_set<int>: ", elems
, true);
124 std::vector
<std::string
> elems
;
126 elems
.reserve(nb_elements
);
127 for (int i
= 0; i
!= nb_elements
; ++i
)
129 std::ostringstream ostr
;
130 ostr
<< "string #" << i
<< ' ' << std::string(1000, 'a' + i
);
131 elems
.push_back(ostr
.str());
135 bench("std::unordered_set<string>: ", elems
, false);
136 bench("std::unordered_set<string>: ", elems
, true);