Debian Codegraph @

dwsys/NUMselect.cpp

raw symbols SCIP
1 /* NUMselect.cpp
2 *
3 * Copyright (C) 2025 David Weenink
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "NUMselect.h"
20 #include "Permutation.h"
21
22 void timeMedian () {
23 try {
24 Melder_clearInfo ();
25 autoINTVEC numberOfElements {11_integer, 101_integer, 1001_integer, 10001_integer, 100001_integer};
26 MelderInfo_writeLine (U"Old: O(n log(n)); sort, NUMquantile(0.5)\n"
27 "New: Alexandrescu (2017) O(n)\n"
28 );
29 MelderInfo_writeLine (U"n tNew tOld tOld/tNew info");
30 for (integer index = 1; index <= numberOfElements.size; index ++) {
31 /*
32 median of increasing values
33 */
34 const integer n = numberOfElements [index];
35 autoVEC p = to_VEC (n);
36 autoVEC pc = copy_VEC (p.get());
37 Melder_stopwatch ();
38 sort_VEC_inout (pc.get());
39 double median1 = NUMquantile (pc.get(), 0.5);
40 double t1 = Melder_stopwatch ();
41 double median2 = num::NUMquantile_e (p.get(), 0.5);
42 double t2 = Melder_stopwatch ();
43 Melder_assert (median1 == median2);
44 MelderInfo_writeLine (n, U" ", t2, U" ", t1, U" *", t1 / t2, U"* increasing 1..n");
45 /*
46 median of decreasing values
47 */
48 for (integer i = 1; i <= n; i ++) {
49 p [i] = n + 1 - i;
50 }
51 pc = copy_VEC (p.get());
52 Melder_stopwatch ();
53 sort_VEC_inout (pc.get());
54 median1 = NUMquantile (pc.get(), 0.5);
55 t1 = Melder_stopwatch ();
56 median2 = num::NUMquantile_e (p.get(), 0.5);
57 t2 = Melder_stopwatch ();
58 Melder_assert (median1 == median2);
59 MelderInfo_writeLine (n, U" ", t2, U" ", t1, U" *", t1 / t2, U"* decreasing n..1");
60 /*
61 median of increasing values followed by decreasing values
62 */
63 for (integer i = 1; i <= n/2; i ++) {
64 p [i] = i;
65 p [n/2 + i] = n/2 + 1 - i;
66 }
67 pc = copy_VEC (p.get());
68 Melder_stopwatch ();
69 sort_VEC_inout (pc.get());
70 median1 = NUMquantile (pc.get(), 0.5);
71 t1 = Melder_stopwatch ();
72 median2 = num::NUMquantile_e (p.get(), 0.5);
73 t2 = Melder_stopwatch ();
74 Melder_assert (median1 == median2);
75 MelderInfo_writeLine (n, U" ", t2, U" ", t1, U" *", t1 / t2, U"* inc-dec");
76 /*
77 median of uniform random values
78 */
79 for (integer i = 1; i <= n; i ++) {
80 p [i] = NUMrandomUniform (-100.0, 100.0);
81 }
82 pc = copy_VEC (p.get());
83 Melder_stopwatch ();
84 sort_VEC_inout (pc.get());
85 median1 = NUMquantile (pc.get(), 0.5);
86 t1 = Melder_stopwatch ();
87 median2 = num::NUMquantile_e (p.get(), 0.5);
88 t2 = Melder_stopwatch ();
89 Melder_assert (median1 == median2);
90 MelderInfo_writeLine (n, U" ", t2, U" ", t1, U" *", t1 / t2, U"* uniform(-100,100)");
91 /*
92 median of Gaussian random values
93 */
94 for (integer i = 1; i <= n; i ++) {
95 p [i] = NUMrandomGauss (0.0, 1.0);
96 }
97 pc = copy_VEC (p.get());
98 Melder_stopwatch ();
99 sort_VEC_inout (pc.get());
100 median1 = NUMquantile (pc.get(), 0.5);
101 t1 = Melder_stopwatch ();
102 median2 = num::NUMquantile_e (p.get(), 0.5);
103 t2 = Melder_stopwatch ();
104 Melder_assert (median1 == median2);
105 MelderInfo_writeLine (n, U" ", t2, U" ", t1, U" *", t1 / t2, U"* gauss(0,1)");
106 }
107 MelderInfo_close ();
108 } catch (MelderError) {
109 Melder_throw (U"Could not perform timing of medians.");
110 }
111 }
112
113 /* End of file NUMselect.cpp */