pystone.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #! /usr/bin/env python
  2. """
  3. "PYSTONE" Benchmark Program
  4. Version: Python/1.1 (corresponds to C/1.1 plus 2 Pystone fixes)
  5. Author: Reinhold P. Weicker, CACM Vol 27, No 10, 10/84 pg. 1013.
  6. Translated from ADA to C by Rick Richardson.
  7. Every method to preserve ADA-likeness has been used,
  8. at the expense of C-ness.
  9. Translated from C to Python by Guido van Rossum.
  10. Version History:
  11. Version 1.1 corrects two bugs in version 1.0:
  12. First, it leaked memory: in Proc1(), NextRecord ends
  13. up having a pointer to itself. I have corrected this
  14. by zapping NextRecord.PtrComp at the end of Proc1().
  15. Second, Proc3() used the operator != to compare a
  16. record to None. This is rather inefficient and not
  17. true to the intention of the original benchmark (where
  18. a pointer comparison to None is intended; the !=
  19. operator attempts to find a method __cmp__ to do value
  20. comparison of the record). Version 1.1 runs 5-10
  21. percent faster than version 1.0, so benchmark figures
  22. of different versions can't be compared directly.
  23. """
  24. LOOPS = 50000
  25. from time import clock
  26. __version__ = "1.1"
  27. [Ident1, Ident2, Ident3, Ident4, Ident5] = range(1, 6)
  28. class Record:
  29. def __init__(self, PtrComp = None, Discr = 0, EnumComp = 0,
  30. IntComp = 0, StringComp = 0):
  31. self.PtrComp = PtrComp
  32. self.Discr = Discr
  33. self.EnumComp = EnumComp
  34. self.IntComp = IntComp
  35. self.StringComp = StringComp
  36. def copy(self):
  37. return Record(self.PtrComp, self.Discr, self.EnumComp,
  38. self.IntComp, self.StringComp)
  39. TRUE = 1
  40. FALSE = 0
  41. def main(loops=LOOPS):
  42. benchtime, stones = pystones(loops)
  43. print "Pystone(%s) time for %d passes = %g" % \
  44. (__version__, loops, benchtime)
  45. print "This machine benchmarks at %g pystones/second" % stones
  46. def pystones(loops=LOOPS):
  47. return Proc0(loops)
  48. IntGlob = 0
  49. BoolGlob = FALSE
  50. Char1Glob = '\0'
  51. Char2Glob = '\0'
  52. Array1Glob = [0]*51
  53. Array2Glob = map(lambda x: x[:], [Array1Glob]*51)
  54. PtrGlb = None
  55. PtrGlbNext = None
  56. def Proc0(loops=LOOPS):
  57. global IntGlob
  58. global BoolGlob
  59. global Char1Glob
  60. global Char2Glob
  61. global Array1Glob
  62. global Array2Glob
  63. global PtrGlb
  64. global PtrGlbNext
  65. starttime = clock()
  66. for i in range(loops):
  67. pass
  68. nulltime = clock() - starttime
  69. PtrGlbNext = Record()
  70. PtrGlb = Record()
  71. PtrGlb.PtrComp = PtrGlbNext
  72. PtrGlb.Discr = Ident1
  73. PtrGlb.EnumComp = Ident3
  74. PtrGlb.IntComp = 40
  75. PtrGlb.StringComp = "DHRYSTONE PROGRAM, SOME STRING"
  76. String1Loc = "DHRYSTONE PROGRAM, 1'ST STRING"
  77. Array2Glob[8][7] = 10
  78. starttime = clock()
  79. for i in range(loops):
  80. Proc5()
  81. Proc4()
  82. IntLoc1 = 2
  83. IntLoc2 = 3
  84. String2Loc = "DHRYSTONE PROGRAM, 2'ND STRING"
  85. EnumLoc = Ident2
  86. BoolGlob = not Func2(String1Loc, String2Loc)
  87. while IntLoc1 < IntLoc2:
  88. IntLoc3 = 5 * IntLoc1 - IntLoc2
  89. IntLoc3 = Proc7(IntLoc1, IntLoc2)
  90. IntLoc1 = IntLoc1 + 1
  91. Proc8(Array1Glob, Array2Glob, IntLoc1, IntLoc3)
  92. PtrGlb = Proc1(PtrGlb)
  93. CharIndex = 'A'
  94. while CharIndex <= Char2Glob:
  95. if EnumLoc == Func1(CharIndex, 'C'):
  96. EnumLoc = Proc6(Ident1)
  97. CharIndex = chr(ord(CharIndex)+1)
  98. IntLoc3 = IntLoc2 * IntLoc1
  99. IntLoc2 = IntLoc3 / IntLoc1
  100. IntLoc2 = 7 * (IntLoc3 - IntLoc2) - IntLoc1
  101. IntLoc1 = Proc2(IntLoc1)
  102. benchtime = clock() - starttime - nulltime
  103. if benchtime == 0.0:
  104. loopsPerBenchtime = 0.0
  105. else:
  106. loopsPerBenchtime = (loops / benchtime)
  107. return benchtime, loopsPerBenchtime
  108. def Proc1(PtrParIn):
  109. PtrParIn.PtrComp = NextRecord = PtrGlb.copy()
  110. PtrParIn.IntComp = 5
  111. NextRecord.IntComp = PtrParIn.IntComp
  112. NextRecord.PtrComp = PtrParIn.PtrComp
  113. NextRecord.PtrComp = Proc3(NextRecord.PtrComp)
  114. if NextRecord.Discr == Ident1:
  115. NextRecord.IntComp = 6
  116. NextRecord.EnumComp = Proc6(PtrParIn.EnumComp)
  117. NextRecord.PtrComp = PtrGlb.PtrComp
  118. NextRecord.IntComp = Proc7(NextRecord.IntComp, 10)
  119. else:
  120. PtrParIn = NextRecord.copy()
  121. NextRecord.PtrComp = None
  122. return PtrParIn
  123. def Proc2(IntParIO):
  124. IntLoc = IntParIO + 10
  125. while 1:
  126. if Char1Glob == 'A':
  127. IntLoc = IntLoc - 1
  128. IntParIO = IntLoc - IntGlob
  129. EnumLoc = Ident1
  130. if EnumLoc == Ident1:
  131. break
  132. return IntParIO
  133. def Proc3(PtrParOut):
  134. global IntGlob
  135. if PtrGlb is not None:
  136. PtrParOut = PtrGlb.PtrComp
  137. else:
  138. IntGlob = 100
  139. PtrGlb.IntComp = Proc7(10, IntGlob)
  140. return PtrParOut
  141. def Proc4():
  142. global Char2Glob
  143. BoolLoc = Char1Glob == 'A'
  144. BoolLoc = BoolLoc or BoolGlob
  145. Char2Glob = 'B'
  146. def Proc5():
  147. global Char1Glob
  148. global BoolGlob
  149. Char1Glob = 'A'
  150. BoolGlob = FALSE
  151. def Proc6(EnumParIn):
  152. EnumParOut = EnumParIn
  153. if not Func3(EnumParIn):
  154. EnumParOut = Ident4
  155. if EnumParIn == Ident1:
  156. EnumParOut = Ident1
  157. elif EnumParIn == Ident2:
  158. if IntGlob > 100:
  159. EnumParOut = Ident1
  160. else:
  161. EnumParOut = Ident4
  162. elif EnumParIn == Ident3:
  163. EnumParOut = Ident2
  164. elif EnumParIn == Ident4:
  165. pass
  166. elif EnumParIn == Ident5:
  167. EnumParOut = Ident3
  168. return EnumParOut
  169. def Proc7(IntParI1, IntParI2):
  170. IntLoc = IntParI1 + 2
  171. IntParOut = IntParI2 + IntLoc
  172. return IntParOut
  173. def Proc8(Array1Par, Array2Par, IntParI1, IntParI2):
  174. global IntGlob
  175. IntLoc = IntParI1 + 5
  176. Array1Par[IntLoc] = IntParI2
  177. Array1Par[IntLoc+1] = Array1Par[IntLoc]
  178. Array1Par[IntLoc+30] = IntLoc
  179. for IntIndex in range(IntLoc, IntLoc+2):
  180. Array2Par[IntLoc][IntIndex] = IntLoc
  181. Array2Par[IntLoc][IntLoc-1] = Array2Par[IntLoc][IntLoc-1] + 1
  182. Array2Par[IntLoc+20][IntLoc] = Array1Par[IntLoc]
  183. IntGlob = 5
  184. def Func1(CharPar1, CharPar2):
  185. CharLoc1 = CharPar1
  186. CharLoc2 = CharLoc1
  187. if CharLoc2 != CharPar2:
  188. return Ident1
  189. else:
  190. return Ident2
  191. def Func2(StrParI1, StrParI2):
  192. IntLoc = 1
  193. while IntLoc <= 1:
  194. if Func1(StrParI1[IntLoc], StrParI2[IntLoc+1]) == Ident1:
  195. CharLoc = 'A'
  196. IntLoc = IntLoc + 1
  197. if CharLoc >= 'W' and CharLoc <= 'Z':
  198. IntLoc = 7
  199. if CharLoc == 'X':
  200. return TRUE
  201. else:
  202. if StrParI1 > StrParI2:
  203. IntLoc = IntLoc + 7
  204. return TRUE
  205. else:
  206. return FALSE
  207. def Func3(EnumParIn):
  208. EnumLoc = EnumParIn
  209. if EnumLoc == Ident3: return TRUE
  210. return FALSE
  211. if __name__ == '__main__':
  212. import sys
  213. def error(msg):
  214. print >>sys.stderr, msg,
  215. print >>sys.stderr, "usage: %s [number_of_loops]" % sys.argv[0]
  216. sys.exit(100)
  217. nargs = len(sys.argv) - 1
  218. if nargs > 1:
  219. error("%d arguments are too many;" % nargs)
  220. elif nargs == 1:
  221. try: loops = int(sys.argv[1])
  222. except ValueError:
  223. error("Invalid argument %r;" % sys.argv[1])
  224. else:
  225. loops = LOOPS
  226. main(loops)