You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.7 KiB

Merged revisions 55007-55179 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/p3yk ........ r55077 | guido.van.rossum | 2007-05-02 11:54:37 -0700 (Wed, 02 May 2007) | 2 lines Use the new print syntax, at least. ........ r55142 | fred.drake | 2007-05-04 21:27:30 -0700 (Fri, 04 May 2007) | 1 line remove old cruftiness ........ r55143 | fred.drake | 2007-05-04 21:52:16 -0700 (Fri, 04 May 2007) | 1 line make this work with the new Python ........ r55162 | neal.norwitz | 2007-05-06 22:29:18 -0700 (Sun, 06 May 2007) | 1 line Get asdl code gen working with Python 2.3. Should continue to work with 3.0 ........ r55164 | neal.norwitz | 2007-05-07 00:00:38 -0700 (Mon, 07 May 2007) | 1 line Verify checkins to p3yk (sic) branch go to 3000 list. ........ r55166 | neal.norwitz | 2007-05-07 00:12:35 -0700 (Mon, 07 May 2007) | 1 line Fix this test so it runs again by importing warnings_test properly. ........ r55167 | neal.norwitz | 2007-05-07 01:03:22 -0700 (Mon, 07 May 2007) | 8 lines So long xrange. range() now supports values that are outside -sys.maxint to sys.maxint. floats raise a TypeError. This has been sitting for a long time. It probably has some problems and needs cleanup. Objects/rangeobject.c now uses 4-space indents since it is almost completely new. ........ r55171 | guido.van.rossum | 2007-05-07 10:21:26 -0700 (Mon, 07 May 2007) | 4 lines Fix two tests that were previously depending on significant spaces at the end of a line (and before that on Python 2.x print behavior that has no exact equivalent in 3.0). ........
19 years ago
Merged revisions 55007-55179 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/p3yk ........ r55077 | guido.van.rossum | 2007-05-02 11:54:37 -0700 (Wed, 02 May 2007) | 2 lines Use the new print syntax, at least. ........ r55142 | fred.drake | 2007-05-04 21:27:30 -0700 (Fri, 04 May 2007) | 1 line remove old cruftiness ........ r55143 | fred.drake | 2007-05-04 21:52:16 -0700 (Fri, 04 May 2007) | 1 line make this work with the new Python ........ r55162 | neal.norwitz | 2007-05-06 22:29:18 -0700 (Sun, 06 May 2007) | 1 line Get asdl code gen working with Python 2.3. Should continue to work with 3.0 ........ r55164 | neal.norwitz | 2007-05-07 00:00:38 -0700 (Mon, 07 May 2007) | 1 line Verify checkins to p3yk (sic) branch go to 3000 list. ........ r55166 | neal.norwitz | 2007-05-07 00:12:35 -0700 (Mon, 07 May 2007) | 1 line Fix this test so it runs again by importing warnings_test properly. ........ r55167 | neal.norwitz | 2007-05-07 01:03:22 -0700 (Mon, 07 May 2007) | 8 lines So long xrange. range() now supports values that are outside -sys.maxint to sys.maxint. floats raise a TypeError. This has been sitting for a long time. It probably has some problems and needs cleanup. Objects/rangeobject.c now uses 4-space indents since it is almost completely new. ........ r55171 | guido.van.rossum | 2007-05-07 10:21:26 -0700 (Mon, 07 May 2007) | 4 lines Fix two tests that were previously depending on significant spaces at the end of a line (and before that on Python 2.x print behavior that has no exact equivalent in 3.0). ........
19 years ago
  1. """Sort performance test.
  2. See main() for command line syntax.
  3. See tabulate() for output format.
  4. """
  5. import sys
  6. import time
  7. import random
  8. import marshal
  9. import tempfile
  10. import os
  11. td = tempfile.gettempdir()
  12. def randfloats(n):
  13. """Return a list of n random floats in [0, 1)."""
  14. # Generating floats is expensive, so this writes them out to a file in
  15. # a temp directory. If the file already exists, it just reads them
  16. # back in and shuffles them a bit.
  17. fn = os.path.join(td, "rr%06d" % n)
  18. try:
  19. fp = open(fn, "rb")
  20. except OSError:
  21. r = random.random
  22. result = [r() for i in range(n)]
  23. try:
  24. try:
  25. fp = open(fn, "wb")
  26. marshal.dump(result, fp)
  27. fp.close()
  28. fp = None
  29. finally:
  30. if fp:
  31. try:
  32. os.unlink(fn)
  33. except OSError:
  34. pass
  35. except OSError as msg:
  36. print("can't write", fn, ":", msg)
  37. else:
  38. result = marshal.load(fp)
  39. fp.close()
  40. # Shuffle it a bit...
  41. for i in range(10):
  42. i = random.randrange(n)
  43. temp = result[:i]
  44. del result[:i]
  45. temp.reverse()
  46. result.extend(temp)
  47. del temp
  48. assert len(result) == n
  49. return result
  50. def flush():
  51. sys.stdout.flush()
  52. def doit(L):
  53. t0 = time.perf_counter()
  54. L.sort()
  55. t1 = time.perf_counter()
  56. print("%6.2f" % (t1-t0), end=' ')
  57. flush()
  58. def tabulate(r):
  59. """Tabulate sort speed for lists of various sizes.
  60. The sizes are 2**i for i in r (the argument, a list).
  61. The output displays i, 2**i, and the time to sort arrays of 2**i
  62. floating point numbers with the following properties:
  63. *sort: random data
  64. \sort: descending data
  65. /sort: ascending data
  66. 3sort: ascending, then 3 random exchanges
  67. +sort: ascending, then 10 random at the end
  68. %sort: ascending, then randomly replace 1% of the elements w/ random values
  69. ~sort: many duplicates
  70. =sort: all equal
  71. !sort: worst case scenario
  72. """
  73. cases = tuple([ch + "sort" for ch in r"*\/3+%~=!"])
  74. fmt = ("%2s %7s" + " %6s"*len(cases))
  75. print(fmt % (("i", "2**i") + cases))
  76. for i in r:
  77. n = 1 << i
  78. L = randfloats(n)
  79. print("%2d %7d" % (i, n), end=' ')
  80. flush()
  81. doit(L) # *sort
  82. L.reverse()
  83. doit(L) # \sort
  84. doit(L) # /sort
  85. # Do 3 random exchanges.
  86. for dummy in range(3):
  87. i1 = random.randrange(n)
  88. i2 = random.randrange(n)
  89. L[i1], L[i2] = L[i2], L[i1]
  90. doit(L) # 3sort
  91. # Replace the last 10 with random floats.
  92. if n >= 10:
  93. L[-10:] = [random.random() for dummy in range(10)]
  94. doit(L) # +sort
  95. # Replace 1% of the elements at random.
  96. for dummy in range(n // 100):
  97. L[random.randrange(n)] = random.random()
  98. doit(L) # %sort
  99. # Arrange for lots of duplicates.
  100. if n > 4:
  101. del L[4:]
  102. L = L * (n // 4)
  103. # Force the elements to be distinct objects, else timings can be
  104. # artificially low.
  105. L = list(map(lambda x: --x, L))
  106. doit(L) # ~sort
  107. del L
  108. # All equal. Again, force the elements to be distinct objects.
  109. L = list(map(abs, [-0.5] * n))
  110. doit(L) # =sort
  111. del L
  112. # This one looks like [3, 2, 1, 0, 0, 1, 2, 3]. It was a bad case
  113. # for an older implementation of quicksort, which used the median
  114. # of the first, last and middle elements as the pivot.
  115. half = n // 2
  116. L = list(range(half - 1, -1, -1))
  117. L.extend(range(half))
  118. # Force to float, so that the timings are comparable. This is
  119. # significantly faster if we leave tham as ints.
  120. L = list(map(float, L))
  121. doit(L) # !sort
  122. print()
  123. def main():
  124. """Main program when invoked as a script.
  125. One argument: tabulate a single row.
  126. Two arguments: tabulate a range (inclusive).
  127. Extra arguments are used to seed the random generator.
  128. """
  129. # default range (inclusive)
  130. k1 = 15
  131. k2 = 20
  132. if sys.argv[1:]:
  133. # one argument: single point
  134. k1 = k2 = int(sys.argv[1])
  135. if sys.argv[2:]:
  136. # two arguments: specify range
  137. k2 = int(sys.argv[2])
  138. if sys.argv[3:]:
  139. # derive random seed from remaining arguments
  140. x = 1
  141. for a in sys.argv[3:]:
  142. x = 69069 * x + hash(a)
  143. random.seed(x)
  144. r = range(k1, k2+1) # include the end point
  145. tabulate(r)
  146. if __name__ == '__main__':
  147. main()