8000 New test %sort. This takes a sorted list, picks 1% of the list posit… · python/cpython@d5f4359 · GitHub
[go: up one dir, main page]

Skip to content

Commit d5f4359

Browse files
committed
New test %sort. This takes a sorted list, picks 1% of the list positions
at random, and replaces the elements at those positions with new random values. I was pleasantly surprised by how fast this goes! It's hard to conceive of an algorithm that could special-case for this effectively. Plus it's exactly what happens if a burst of gamma rays corrupts your sorted database on disk <wink>. i 2**i *sort ... %sort 15 32768 0.18 ... 0.03 16 65536 0.24 ... 0.04 17 131072 0.53 ... 0.08 18 262144 1.17 ... 0.16 19 524288 2.56 ... 0.35 20 1048576 5.54 ... 0.77
1 parent fe51c6d commit d5f4359

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Lib/test/sortperf.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,13 @@ def tabulate(r):
7676
/sort: ascending data
7777
3sort: ascending, then 3 random exchanges
7878
+sort: ascending, then 10 random at the end
79+
%sort: ascending, then randomly replace 1% of the elements w/ random values
7980
~sort: many duplicates
8081
=sort: all equal
8182
!sort: worst case scenario
8283
8384
"""
84-
cases = tuple([ch + "sort" for ch in r"*\/3+~=!"])
85+
cases = tuple([ch + "sort" for ch in r"*\/3+%~=!"])
8586
fmt = ("%2s %7s" + " %6s"*len(cases))
8687
print fmt % (("i", "2**i") + cases)
8788
for i in r:
@@ -106,6 +107,11 @@ def tabulate(r):
106107
L[-10:] = [random.random() for dummy in range(10)]
107108
doit(L) # +sort
108109

110+
# Replace 1% of the elements at random.
111+
for dummy in xrange(n // 100):
112+
L[random.randrange(n)] = random.random()
113+
doit(L) # %sort
114+
109115
# Arrange for lots of duplicates.
110116
if n > 4:
111117
del L[4:]

0 commit comments

Comments
 (0)
0