]>
Commit | Line | Data |
---|---|---|
99dee823 | 1 | // Copyright (C) 2013-2021 Free Software Foundation, Inc. |
a10bad86 CJ |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
18 | #include <vector> | |
19 | #include <algorithm> | |
ba23e045 FD |
20 | |
21 | #include <testsuite_new_operators.h> | |
a10bad86 CJ |
22 | #include <testsuite_performance.h> |
23 | ||
ba23e045 FD |
24 | const int max_size = 10000000; |
25 | const int small_size = 200000; | |
26 | ||
27 | void bench(size_t mem_threshold, | |
28 | std::vector<int> revv, | |
29 | std::vector<int> fwdv, | |
30 | std::vector<int> rndv) | |
a10bad86 CJ |
31 | { |
32 | using namespace __gnu_test; | |
33 | ||
34 | time_counter time; | |
35 | resource_counter resource; | |
36 | ||
ba23e045 | 37 | set_new_limit(mem_threshold); |
a10bad86 CJ |
38 | |
39 | start_counters(time, resource); | |
ba23e045 | 40 | std::stable_sort(revv.begin(), revv.end()); |
a10bad86 CJ |
41 | stop_counters(time, resource); |
42 | ||
ba23e045 | 43 | set_new_limit(~size_t(0)); |
a10bad86 CJ |
44 | report_performance(__FILE__, "reverse", time, resource); |
45 | clear_counters(time, resource); | |
46 | ||
ba23e045 | 47 | set_new_limit(mem_threshold); |
a10bad86 CJ |
48 | |
49 | start_counters(time, resource); | |
ba23e045 | 50 | std::stable_sort(fwdv.begin(), fwdv.end()); |
a10bad86 CJ |
51 | stop_counters(time, resource); |
52 | ||
ba23e045 | 53 | set_new_limit(~size_t(0)); |
a10bad86 CJ |
54 | report_performance(__FILE__, "forwards", time, resource); |
55 | clear_counters(time, resource); | |
56 | ||
ba23e045 FD |
57 | start_counters(time, resource); |
58 | std::stable_sort(rndv.begin(), rndv.end()); | |
59 | stop_counters(time, resource); | |
60 | ||
61 | set_new_limit(~size_t(0)); | |
62 | report_performance(__FILE__, "random", time, resource); | |
63 | } | |
64 | ||
65 | int main() | |
66 | { | |
67 | using namespace __gnu_test; | |
68 | ||
69 | // No memory constraint. | |
70 | set_new_limit(~size_t(0)); | |
71 | ||
72 | std::vector<int> revv(max_size); | |
73 | for (int i = 0; i < max_size; ++i) | |
74 | revv[i] = -i; | |
75 | ||
76 | std::vector<int> fwdv(max_size); | |
77 | for (int i = 0; i < max_size; ++i) | |
78 | fwdv[i] = i; | |
79 | ||
80 | // a simple pseudo-random series which does not rely on rand() and friends | |
81 | std::vector<int> rndv(max_size); | |
82 | rndv[0] = 0; | |
a10bad86 | 83 | for (int i = 1; i < max_size; ++i) |
ba23e045 FD |
84 | rndv[i] = (rndv[i-1] + 110211473) * 745988807; |
85 | ||
86 | time_counter time; | |
87 | resource_counter resource; | |
a10bad86 CJ |
88 | |
89 | start_counters(time, resource); | |
ba23e045 | 90 | bench(~size_t(0), revv, fwdv, rndv); |
a10bad86 CJ |
91 | stop_counters(time, resource); |
92 | ||
ba23e045 FD |
93 | report_performance(__FILE__, "bench 1 / 1 memory", time, resource); |
94 | clear_counters(time, resource); | |
95 | ||
96 | start_counters(time, resource); | |
97 | // Limit to fourth the expected size of the sorted array. | |
98 | bench(max_size * sizeof(int) / 4, revv, fwdv, rndv); | |
99 | stop_counters(time, resource); | |
100 | ||
101 | report_performance(__FILE__, "bench 1 / 4 memory", time, resource); | |
102 | clear_counters(time, resource); | |
103 | ||
104 | start_counters(time, resource); | |
105 | // Limit to 1/64 of range size. | |
106 | bench(max_size * sizeof(int) / 64, revv, fwdv, rndv); | |
107 | stop_counters(time, resource); | |
108 | ||
109 | report_performance(__FILE__, "bench 1 /64 memory", time, resource); | |
110 | clear_counters(time, resource); | |
111 | ||
112 | revv.resize(small_size); | |
113 | fwdv.resize(small_size); | |
114 | rndv.resize(small_size); | |
115 | ||
116 | start_counters(time, resource); | |
117 | // Forbid any allocation. | |
118 | bench(0, revv, fwdv, rndv); | |
119 | stop_counters(time, resource); | |
a10bad86 | 120 | |
ba23e045 | 121 | report_performance(__FILE__, "bench 0 / 1 memory", time, resource); |
a10bad86 CJ |
122 | return 0; |
123 | } |