cpython/Lib/test/sortperf.py

138 lines
3.3 KiB
Python
Raw Normal View History

1998-05-10 15:27:29 -03:00
"""Sort performance test.
See main() for command line syntax.
See tabulate() for output format.
"""
1998-05-10 15:20:05 -03:00
import sys
import time
1998-05-20 14:05:52 -03:00
import random
1998-05-10 15:20:05 -03:00
import marshal
import tempfile
import operator
import os
td = tempfile.gettempdir()
def randrange(n):
"""Return a random shuffle of range(n)."""
fn = os.path.join(td, "rr%06d" % n)
try:
fp = open(fn, "rb")
except IOError:
result = []
for i in range(n):
1998-05-20 14:05:52 -03:00
result.append(random.random())
1998-05-10 15:20:05 -03:00
try:
try:
fp = open(fn, "wb")
marshal.dump(result, fp)
fp.close()
fp = None
finally:
if fp:
try:
os.unlink(fn)
except os.error:
pass
except IOError, msg:
print "can't write", fn, ":", msg
else:
result = marshal.load(fp)
fp.close()
##assert len(result) == n
# Shuffle it a bit...
for i in range(10):
1998-05-20 14:05:52 -03:00
i = random.randint(0, n-1)
1998-05-10 15:20:05 -03:00
temp = result[:i]
del result[:i]
temp.reverse()
result[len(result):] = temp
del temp
return result
def fl():
sys.stdout.flush()
def doit(L):
t0 = time.clock()
L.sort()
t1 = time.clock()
print "%6.2f" % (t1-t0),
fl()
def tabulate(r):
1998-05-10 15:27:29 -03:00
"""Tabulate sort speed for lists of various sizes.
The sizes are 2**i for i in r (the argument, a list).
The output displays i, 2**i, and the time to sort arrays of 2**i
floating point numbers with the following properties:
*sort: random data
\sort: descending data
/sort: ascending data
~sort: many duplicates
-sort: all equal
"""
1998-05-10 15:20:05 -03:00
fmt = ("%2s %6s" + " %6s"*5)
print fmt % ("i", "2**i", "*sort", "\\sort", "/sort", "~sort", "-sort")
for i in r:
n = 1<<i
L = randrange(n)
##assert len(L) == n
print "%2d %6d" % (i, n),
fl()
doit(L) # *sort
L.reverse()
doit(L) # \sort
doit(L) # /sort
if n > 4:
1998-05-12 10:21:31 -03:00
del L[4:]
L = L*(n/4)
L = map(lambda x: --x, L)
1998-05-10 15:20:05 -03:00
doit(L) # ~sort
1998-05-12 10:21:31 -03:00
del L
1998-05-10 15:20:05 -03:00
L = map(abs, [-0.5]*n)
doit(L) # -sort
print
def main():
1998-05-10 15:27:29 -03:00
"""Main program when invoked as a script.
One argument: tabulate a single row.
Two arguments: tabulate a range (inclusive).
Extra arguments are used to seed the random generator.
"""
1998-05-10 15:20:05 -03:00
import string
# default range (inclusive)
k1 = 15
k2 = 19
if sys.argv[1:]:
1998-05-10 15:27:29 -03:00
# one argument: single point
k1 = k2 = string.atoi(sys.argv[1])
1998-05-10 15:20:05 -03:00
if sys.argv[2:]:
1998-05-10 15:27:29 -03:00
# two arguments: specify range
1998-05-10 15:20:05 -03:00
k2 = string.atoi(sys.argv[2])
if sys.argv[3:]:
# derive random seed from remaining arguments
x, y, z = 0, 0, 0
for a in sys.argv[3:]:
h = hash(a)
h, d = divmod(h, 256)
h = h & 0xffffff
x = (x^h^d) & 255
h = h>>8
y = (y^h^d) & 255
h = h>>8
z = (z^h^d) & 255
1998-05-20 14:05:52 -03:00
random.seed(x, y, z)
1998-05-10 15:27:29 -03:00
r = range(k1, k2+1) # include the end point
1998-05-10 15:20:05 -03:00
tabulate(r)
if __name__ == '__main__':
main()