summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/performance/23_containers/insert_erase/unordered_small_size.cc
blob: ae63c15b5da90eb217ae30ee49e776e5d98f800f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// { dg-do run { target c++11 } }

// Copyright (C) 2021 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

#include <string>
#include <sstream>
#include <vector>
#include <unordered_set>
#include <testsuite_performance.h>

namespace
{
  const int nb_elements = 20;
  const int nb_insts = 150000;

  template<typename _ElemType>
    void bench(const char* desc, const std::vector<_ElemType>& elems)
    {
      using namespace __gnu_test;

      time_counter time;
      resource_counter resource;

      std::vector<std::unordered_set<_ElemType>> insts(nb_insts);
      for (int j = 0; j != nb_insts; ++j)
	insts.emplace_back();

      start_counters(time, resource);

      for (auto& us : insts)
	for (int i = 0; i != nb_elements; ++i)
	  us.insert(elems[i]);

      stop_counters(time, resource);

      std::ostringstream ostr;
      ostr << desc << " 1st insert";
      report_performance(__FILE__, ostr.str().c_str(), time, resource);

      start_counters(time, resource);

      for (auto& us : insts)
	for (int i = nb_elements - 1; i >= 0; --i)
	  {
	    auto it = us.find(elems[i]);
	    if (it != us.end())
	      us.erase(it);
	  }

      stop_counters(time, resource);

      ostr.str("");
      ostr << desc << " find/erase";
      report_performance(__FILE__, ostr.str().c_str(), time, resource);

      start_counters(time, resource);

      for (auto& us : insts)
	{
	  us.insert(elems[0]);
	  for (int i = nb_elements - 1; i >= 0; --i)
	    us.insert(elems[i]);
	}

      stop_counters(time, resource);
      ostr.str("");
      ostr << desc << " 2nd insert";
      report_performance(__FILE__, ostr.str().c_str(), time, resource);

      start_counters(time, resource);

      for (auto& us : insts)
	for (int j = nb_elements - 1; j >= 0; --j)
	  us.erase(elems[j]);

      stop_counters(time, resource);
      ostr.str("");
      ostr << desc << " erase key ";
      report_performance(__FILE__, ostr.str().c_str(), time, resource);
    }
}

int main()
{
  {
    std::vector<int> elems;
    elems.reserve(nb_elements);
    for (int i = 0; i != nb_elements; ++i)
      elems.push_back(i);

    bench("std::unordered_set<int>:    ", elems);
  }

  {
    std::vector<std::string> elems;
    {
      elems.reserve(nb_elements);
      for (int i = 0; i != nb_elements; ++i)
	{
	  std::ostringstream ostr;
	  ostr << "string #" << i << ' ' << std::string(1000, 'a' + i);
	  elems.push_back(ostr.str());
	}
    }

    bench("std::unordered_set<string>: ", elems);
  }

  return 0;
}