1 """Sort performance test.
3 See main() for command line syntax.
4 See tabulate() for output format.
16 td
= tempfile
.gettempdir()
19 """Return a random shuffle of range(n)."""
20 fn
= os
.path
.join(td
, "rr%06d" % n
)
26 result
.append(whrandom
.random())
30 marshal
.dump(result
, fp
)
40 print "can't write", fn
, ":", msg
42 result
= marshal
.load(fp
)
44 ##assert len(result) == n
47 i
= whrandom
.randint(0, n
-1)
51 result
[len(result
):] = temp
62 print "%6.2f" % (t1
-t0
),
66 """Tabulate sort speed for lists of various sizes.
68 The sizes are 2**i for i in r (the argument, a list).
70 The output displays i, 2**i, and the time to sort arrays of 2**i
71 floating point numbers with the following properties:
74 \sort: descending data
76 ~sort: many duplicates
78 !sort: worst case scenario
81 cases
= ("*sort", "\\sort", "/sort", "~sort", "-sort", "!sort")
82 fmt
= ("%2s %6s" + " %6s"*len(cases
))
83 print fmt
% (("i", "2**i") + cases
)
88 print "%2d %6d" % (i
, n
),
97 L
= map(lambda x
: --x
, L
)
100 L
= map(abs, [-0.5]*n
)
102 L
= range(n
/2-1, -1, -1)
103 L
[len(L
):] = range(n
/2)
108 """Main program when invoked as a script.
110 One argument: tabulate a single row.
111 Two arguments: tabulate a range (inclusive).
112 Extra arguments are used to seed the random generator.
116 # default range (inclusive)
120 # one argument: single point
121 k1
= k2
= string
.atoi(sys
.argv
[1])
123 # two arguments: specify range
124 k2
= string
.atoi(sys
.argv
[2])
126 # derive random seed from remaining arguments
128 for a
in sys
.argv
[3:]:
130 h
, d
= divmod(h
, 256)
137 whrandom
.seed(x
, y
, z
)
138 r
= range(k1
, k2
+1) # include the end point
141 if __name__
== '__main__':