Commit fd38fd6
committed
sort: immediately compare whole lines if they parse as numbers
Numeric sort can be relatively slow on inputs that are wholly or
mostly numbers. This is more clear when comparing with the speed of
GeneralNumeric.
This change parses whole lines as f64 and stores that info in
`LineData`. This is faster than doing the parsing two lines at
a time in `compare_by()`.
# Benchmarks
`shuf -i 1-1000000 -n 1000000 > /tmp/shuffled.txt`
% hyperfine --warmup 3 \
'/tmp/gnu-sort -n /tmp/shuffled.txt'
'/tmp/before_coreutils sort -n /tmp/shuffled.txt'
'/tmp/after_coreutils sort -n /tmp/shuffled.txt'
Benchmark 1: /tmp/gnu-sort -n /tmp/shuffled.txt
Time (mean ± σ): 198.2 ms ± 5.8 ms [User: 884.6 ms, System: 22.0 ms]
Range (min … max): 187.3 ms … 207.4 ms 15 runs
Benchmark 2: /tmp/before_coreutils sort -n /tmp/shuffled.txt
Time (mean ± σ): 361.3 ms ± 8.7 ms [User: 1898.7 ms, System: 18.9 ms]
Range (min … max): 350.4 ms … 375.3 ms 10 runs
Benchmark 3: /tmp/after_coreutils sort -n /tmp/shuffled.txt
Time (mean ± σ): 175.1 ms ± 6.7 ms [User: 536.8 ms, System: 21.6 ms]
Range (min … max): 169.3 ms … 197.0 ms 16 runs
Summary
/tmp/after_coreutils sort -n /tmp/shuffled.txt ran
1.13 ± 0.05 times faster than /tmp/gnu-sort -n /tmp/shuffled.txt
2.06 ± 0.09 times faster than /tmp/before_coreutils sort -n /tmp/shuffled.txt
Signed-off-by: Mohammad AlSaleh <CE.Mohammad.AlSaleh@gmail.com>1 parent d33d731 commit fd38fd6
2 files changed
+34
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| |||
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| 56 | + | |
55 | 57 | | |
56 | 58 | | |
57 | 59 | | |
| |||
73 | 75 | | |
74 | 76 | | |
75 | 77 | | |
| 78 | + | |
76 | 79 | | |
77 | 80 | | |
78 | 81 | | |
79 | 82 | | |
80 | 83 | | |
81 | 84 | | |
82 | 85 | | |
| 86 | + | |
83 | 87 | | |
84 | 88 | | |
85 | 89 | | |
| |||
97 | 101 | | |
98 | 102 | | |
99 | 103 | | |
| 104 | + | |
100 | 105 | | |
101 | 106 | | |
102 | 107 | | |
| |||
107 | 112 | | |
108 | 113 | | |
109 | 114 | | |
| 115 | + | |
110 | 116 | | |
111 | 117 | | |
112 | 118 | | |
| |||
149 | 155 | | |
150 | 156 | | |
151 | 157 | | |
| 158 | + | |
152 | 159 | | |
153 | 160 | | |
154 | 161 | | |
| |||
184 | 191 | | |
185 | 192 | | |
186 | 193 | | |
| 194 | + | |
187 | 195 | | |
188 | 196 | | |
189 | 197 | | |
| |||
207 | 215 | | |
208 | 216 | | |
209 | 217 | | |
| 218 | + | |
210 | 219 | | |
211 | 220 | | |
212 | 221 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
460 | 460 | | |
461 | 461 | | |
462 | 462 | | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
463 | 470 | | |
464 | 471 | | |
465 | 472 | | |
| |||
1563 | 1570 | | |
1564 | 1571 | | |
1565 | 1572 | | |
| 1573 | + | |
| 1574 | + | |
| 1575 | + | |
| 1576 | + | |
| 1577 | + | |
| 1578 | + | |
| 1579 | + | |
| 1580 | + | |
| 1581 | + | |
| 1582 | + | |
| 1583 | + | |
| 1584 | + | |
| 1585 | + | |
| 1586 | + | |
| 1587 | + | |
| 1588 | + | |
| 1589 | + | |
| 1590 | + | |
1566 | 1591 | | |
1567 | 1592 | | |
1568 | 1593 | | |
| |||
0 commit comments