1 // Copyright (C) 2013-2024 Free Software Foundation, Inc.
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)
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.
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/>.
21 #include <testsuite_new_operators.h>
22 #include <testsuite_performance.h>
24 const int max_size
= 10000000;
25 const int small_size
= 200000;
27 void bench(size_t mem_threshold
,
28 std::vector
<int> revv
,
29 std::vector
<int> fwdv
,
30 std::vector
<int> rndv
)
32 using namespace __gnu_test
;
35 resource_counter resource
;
37 set_new_limit(mem_threshold
);
39 start_counters(time
, resource
);
40 std::stable_sort(revv
.begin(), revv
.end());
41 stop_counters(time
, resource
);
43 set_new_limit(~size_t(0));
44 report_performance(__FILE__
, "reverse", time
, resource
);
45 clear_counters(time
, resource
);
47 set_new_limit(mem_threshold
);
49 start_counters(time
, resource
);
50 std::stable_sort(fwdv
.begin(), fwdv
.end());
51 stop_counters(time
, resource
);
53 set_new_limit(~size_t(0));
54 report_performance(__FILE__
, "forwards", time
, resource
);
55 clear_counters(time
, resource
);
57 start_counters(time
, resource
);
58 std::stable_sort(rndv
.begin(), rndv
.end());
59 stop_counters(time
, resource
);
61 set_new_limit(~size_t(0));
62 report_performance(__FILE__
, "random", time
, resource
);
67 using namespace __gnu_test
;
69 // No memory constraint.
70 set_new_limit(~size_t(0));
72 std::vector
<int> revv(max_size
);
73 for (int i
= 0; i
< max_size
; ++i
)
76 std::vector
<int> fwdv(max_size
);
77 for (int i
= 0; i
< max_size
; ++i
)
80 // a simple pseudo-random series which does not rely on rand() and friends
81 std::vector
<int> rndv(max_size
);
83 for (int i
= 1; i
< max_size
; ++i
)
84 rndv
[i
] = (rndv
[i
-1] + 110211473) * 745988807;
87 resource_counter resource
;
89 start_counters(time
, resource
);
90 bench(~size_t(0), revv
, fwdv
, rndv
);
91 stop_counters(time
, resource
);
93 report_performance(__FILE__
, "bench 1 / 1 memory", time
, resource
);
94 clear_counters(time
, resource
);
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
);
101 report_performance(__FILE__
, "bench 1 / 4 memory", time
, resource
);
102 clear_counters(time
, resource
);
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
);
109 report_performance(__FILE__
, "bench 1 /64 memory", time
, resource
);
110 clear_counters(time
, resource
);
112 revv
.resize(small_size
);
113 fwdv
.resize(small_size
);
114 rndv
.resize(small_size
);
116 start_counters(time
, resource
);
117 // Forbid any allocation.
118 bench(0, revv
, fwdv
, rndv
);
119 stop_counters(time
, resource
);
121 report_performance(__FILE__
, "bench 0 / 1 memory", time
, resource
);