8000 New test "+sort", tacking 10 random floats on to the end of a sorted · python/cpython@7ea39b1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ea39b1

Browse files
committed
New test "+sort", tacking 10 random floats on to the end of a sorted
array. Our samplesort special-cases the snot out of this, running about 12x faster than *sort. The experimental mergesort runs it about 8x faster than *sort without special-casing, but should really do better than that (when merging runs of different lengths, right now it only does something clever about finding where the second run begins in the first and where the first run ends in the second, and that's more of a temp-memory optimization).
1 parent 53d019c commit 7ea39b1

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

Lib/test/sortperf.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,14 @@ def tabulate(r):
7474
*sort: random data
7575
\sort: descending data
7676
/sort: ascending data
77-
3sort: ascending data but with 3 random exchanges
77+
3sort: ascending, then 3 random exchanges
78+
+sort: ascending, then 10 random at the end
7879
~sort: many duplicates
7980
=sort: all equal
8081
!sort: worst case scenario
8182
8283
"""
83-
cases = ("*sort", "\\sort", "/sort", "3sort", "~sort", "=sort", "!sort")
84+
cases = tuple([ch + "sort" for ch in r"*\/3+~=!"])
8485
fmt = ("%2s %7s" + " %6s"*len(cases))
8586
print fmt % (("i", "2**i") + cases)
8687
for i in r:
@@ -100,6 +101,11 @@ def tabulate(r):
100101
L[i1], L[i2] = L[i2], L[i1]
101102
doit(L) # 3sort
102103

104+
# Replace the last 10 with random floats.
105+
if n >= 10:
106+
L[-10:] = [random.random() for dummy in range(10)]
107+
doit(L) # +sort
108+
103109
# Arrange for lots of duplicates.
104110
if n > 4:
105111
del L[4:]
@@ -117,10 +123,7 @@ def tabulate(r):
117123

118124
# This one looks like [3, 2, 1, 0, 0, 1, 2, 3]. It was a bad case
119125
# for an older implementation of quicksort, which used the median
120-
# of the first, last and middle elements as the pivot. It's still
121-
# a worse-than-average case for samplesort, but on the order of a
122-
# measly 5% worse, not a quadratic-time disaster as it was with
123-
# quicksort.
126+
# of the first, last and middle elements as the pivot.
124127
half = n // 2
125128
L = range(half - 1, -1, -1)
126129
L.extend(range(half))

0 commit comments

Comments
 (0)
0