summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/performance/25_algorithms/stable_sort.cc
blob: 59449398de714e5d2624f627b53408f5bccd8c66 (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
// Copyright (C) 2013-2022 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 <vector>
#include <algorithm>

#include <testsuite_new_operators.h>
#include <testsuite_performance.h>

const int max_size = 10000000;
const int small_size = 200000;

void bench(size_t mem_threshold,
	   std::vector<int> revv,
	   std::vector<int> fwdv,
	   std::vector<int> rndv)
{
  using namespace __gnu_test;

  time_counter time;
  resource_counter resource;

  set_new_limit(mem_threshold);

  start_counters(time, resource);
  std::stable_sort(revv.begin(), revv.end());
  stop_counters(time, resource);

  set_new_limit(~size_t(0));
  report_performance(__FILE__, "reverse", time, resource);
  clear_counters(time, resource);

  set_new_limit(mem_threshold);

  start_counters(time, resource);
  std::stable_sort(fwdv.begin(), fwdv.end());
  stop_counters(time, resource);

  set_new_limit(~size_t(0));
  report_performance(__FILE__, "forwards", time, resource);
  clear_counters(time, resource);

  start_counters(time, resource);
  std::stable_sort(rndv.begin(), rndv.end());
  stop_counters(time, resource);

  set_new_limit(~size_t(0));
  report_performance(__FILE__, "random", time, resource);
}

int main()
{
  using namespace __gnu_test;

  // No memory constraint.
  set_new_limit(~size_t(0));

  std::vector<int> revv(max_size);
  for (int i = 0; i < max_size; ++i)
    revv[i] = -i;

  std::vector<int> fwdv(max_size);
  for (int i = 0; i < max_size; ++i)
    fwdv[i] = i;

  // a simple pseudo-random series which does not rely on rand() and friends
  std::vector<int> rndv(max_size);
  rndv[0] = 0;
  for (int i = 1; i < max_size; ++i)
    rndv[i] = (rndv[i-1] + 110211473) * 745988807;

  time_counter time;
  resource_counter resource;

  start_counters(time, resource);
  bench(~size_t(0), revv, fwdv, rndv);
  stop_counters(time, resource);

  report_performance(__FILE__, "bench 1 / 1 memory", time, resource);
  clear_counters(time, resource);

  start_counters(time, resource);
  // Limit to fourth the expected size of the sorted array.
  bench(max_size * sizeof(int) / 4, revv, fwdv, rndv);
  stop_counters(time, resource);

  report_performance(__FILE__, "bench 1 / 4 memory", time, resource);
  clear_counters(time, resource);

  start_counters(time, resource);
  // Limit to 1/64 of range size.
  bench(max_size * sizeof(int) / 64, revv, fwdv, rndv);
  stop_counters(time, resource);

  report_performance(__FILE__, "bench 1 /64 memory", time, resource);
  clear_counters(time, resource);

  revv.resize(small_size);
  fwdv.resize(small_size);
  rndv.resize(small_size);

  start_counters(time, resource);
  // Forbid any allocation.
  bench(0, revv, fwdv, rndv);
  stop_counters(time, resource);

  report_performance(__FILE__, "bench 0 / 1 memory", time, resource);
  return 0;
}