pystone.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/usr/bin/env python3
  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. from __future__ import print_function
  25. from time import clock
  26. LOOPS = 50000
  27. __version__ = "1.1"
  28. [Ident1, Ident2, Ident3, Ident4, Ident5] = range(1, 6)
  29. class Record(object):
  30. def __init__(self, PtrComp = None, Discr = 0, EnumComp = 0,
  31. IntComp = 0, StringComp = 0):
  32. self.PtrComp = PtrComp
  33. self.Discr = Discr
  34. self.EnumComp = EnumComp
  35. self.IntComp = IntComp
  36. self.StringComp = StringComp
  37. def copy(self):
  38. return Record(self.PtrComp, self.Discr, self.EnumComp,
  39. self.IntComp, self.StringComp)
  40. TRUE = 1
  41. FALSE = 0
  42. def main(loops=LOOPS):
  43. benchtime, stones = pystones(loops)
  44. print("Pystone(%s) time for %d passes = %g" % \
  45. (__version__, loops, benchtime))
  46. print("This machine benchmarks at %g pystones/second" % stones)
  47. def pystones(loops=LOOPS):
  48. return Proc0(loops)
  49. IntGlob = 0
  50. BoolGlob = FALSE
  51. Char1Glob = '\0'
  52. Char2Glob = '\0'
  53. Array1Glob = [0]*51
  54. Array2Glob = [x[:] for x in [Array1Glob]*51]
  55. PtrGlb = None
  56. PtrGlbNext = None
  57. def Proc0(loops=LOOPS):
  58. global IntGlob
  59. global BoolGlob
  60. global Char1Glob
  61. global Char2Glob
  62. global Array1Glob
  63. global Array2Glob
  64. global PtrGlb
  65. global PtrGlbNext
  66. starttime = clock()
  67. for i in range(loops):
  68. pass
  69. nulltime = clock() - starttime
  70. PtrGlbNext = Record()
  71. PtrGlb = Record()
  72. PtrGlb.PtrComp = PtrGlbNext
  73. PtrGlb.Discr = Ident1
  74. PtrGlb.EnumComp = Ident3
  75. PtrGlb.IntComp = 40
  76. PtrGlb.StringComp = "DHRYSTONE PROGRAM, SOME STRING"
  77. String1Loc = "DHRYSTONE PROGRAM, 1'ST STRING"
  78. Array2Glob[8][7] = 10
  79. starttime = clock()
  80. for i in range(loops):
  81. Proc5()
  82. Proc4()
  83. IntLoc1 = 2
  84. IntLoc2 = 3
  85. String2Loc = "DHRYSTONE PROGRAM, 2'ND STRING"
  86. EnumLoc = Ident2
  87. BoolGlob = not Func2(String1Loc, String2Loc)
  88. while IntLoc1 < IntLoc2:
  89. IntLoc3 = 5 * IntLoc1 - IntLoc2
  90. IntLoc3 = Proc7(IntLoc1, IntLoc2)
  91. IntLoc1 = IntLoc1 + 1
  92. Proc8(Array1Glob, Array2Glob, IntLoc1, IntLoc3)
  93. PtrGlb = Proc1(PtrGlb)
  94. CharIndex = 'A'
  95. while CharIndex <= Char2Glob:
  96. if EnumLoc == Func1(CharIndex, 'C'):
  97. EnumLoc = Proc6(Ident1)
  98. CharIndex = chr(ord(CharIndex)+1)
  99. IntLoc3 = IntLoc2 * IntLoc1
  100. IntLoc2 = IntLoc3 / IntLoc1
  101. IntLoc2 = 7 * (IntLoc3 - IntLoc2) - IntLoc1
  102. IntLoc1 = Proc2(IntLoc1)
  103. benchtime = clock() - starttime - nulltime
  104. if benchtime == 0.0:
  105. loopsPerBenchtime = 0.0
  106. else:
  107. loopsPerBenchtime = (loops / benchtime)
  108. return benchtime, loopsPerBenchtime
  109. def Proc1(PtrParIn):
  110. PtrParIn.PtrComp = NextRecord = PtrGlb.copy()
  111. PtrParIn.IntComp = 5
  112. NextRecord.IntComp = PtrParIn.IntComp
  113. NextRecord.PtrComp = PtrParIn.PtrComp
  114. NextRecord.PtrComp = Proc3(NextRecord.PtrComp)
  115. if NextRecord.Discr == Ident1:
  116. NextRecord.IntComp = 6
  117. NextRecord.EnumComp = Proc6(PtrParIn.EnumComp)
  118. NextRecord.PtrComp = PtrGlb.PtrComp
  119. NextRecord.IntComp = Proc7(NextRecord.IntComp, 10)
  120. else:
  121. PtrParIn = NextRecord.copy()
  122. NextRecord.PtrComp = None
  123. return PtrParIn
  124. def Proc2(IntParIO):
  125. IntLoc = IntParIO + 10
  126. while 1:
  127. if Char1Glob == 'A':
  128. IntLoc = IntLoc - 1
  129. IntParIO = IntLoc - IntGlob
  130. EnumLoc = Ident1
  131. if EnumLoc == Ident1:
  132. break
  133. return IntParIO
  134. def Proc3(PtrParOut):
  135. global IntGlob
  136. if PtrGlb is not None:
  137. PtrParOut = PtrGlb.PtrComp
  138. else:
  139. IntGlob = 100
  140. PtrGlb.IntComp = Proc7(10, IntGlob)
  141. return PtrParOut
  142. def Proc4():
  143. global Char2Glob
  144. BoolLoc = Char1Glob == 'A'
  145. BoolLoc = BoolLoc or BoolGlob
  146. Char2Glob = 'B'
  147. def Proc5():
  148. global Char1Glob
  149. global BoolGlob
  150. Char1Glob = 'A'
  151. BoolGlob = FALSE
  152. def Proc6(EnumParIn):
  153. EnumParOut = EnumParIn
  154. if not Func3(EnumParIn):
  155. EnumParOut = Ident4
  156. if EnumParIn == Ident1:
  157. EnumParOut = Ident1
  158. elif EnumParIn == Ident2:
  159. if IntGlob > 100:
  160. EnumParOut = Ident1
  161. else:
  162. EnumParOut = Ident4
  163. elif EnumParIn == Ident3:
  164. EnumParOut = Ident2
  165. elif EnumParIn == Ident4:
  166. pass
  167. elif EnumParIn == Ident5:
  168. EnumParOut = Ident3
  169. return EnumParOut
  170. def Proc7(IntParI1, IntParI2):
  171. IntLoc = IntParI1 + 2
  172. IntParOut = IntParI2 + IntLoc
  173. return IntParOut
  174. def Proc8(Array1Par, Array2Par, IntParI1, IntParI2):
  175. global IntGlob
  176. IntLoc = IntParI1 + 5
  177. Array1Par[IntLoc] = IntParI2
  178. Array1Par[IntLoc+1] = Array1Par[IntLoc]
  179. Array1Par[IntLoc+30] = IntLoc
  180. for IntIndex in range(IntLoc, IntLoc+2):
  181. Array2Par[IntLoc][IntIndex] = IntLoc
  182. Array2Par[IntLoc][IntLoc-1] = Array2Par[IntLoc][IntLoc-1] + 1
  183. Array2Par[IntLoc+20][IntLoc] = Array1Par[IntLoc]
  184. IntGlob = 5
  185. def Func1(CharPar1, CharPar2):
  186. CharLoc1 = CharPar1
  187. CharLoc2 = CharLoc1
  188. if CharLoc2 != CharPar2:
  189. return Ident1
  190. else:
  191. return Ident2
  192. def Func2(StrParI1, StrParI2):
  193. IntLoc = 1
  194. while IntLoc <= 1:
  195. if Func1(StrParI1[IntLoc], StrParI2[IntLoc+1]) == Ident1:
  196. CharLoc = 'A'
  197. IntLoc = IntLoc + 1
  198. if CharLoc >= 'W' and CharLoc <= 'Z':
  199. IntLoc = 7
  200. if CharLoc == 'X':
  201. return TRUE
  202. else:
  203. if StrParI1 > StrParI2:
  204. IntLoc = IntLoc + 7
  205. return TRUE
  206. else:
  207. return FALSE
  208. def Func3(EnumParIn):
  209. EnumLoc = EnumParIn
  210. if EnumLoc == Ident3: return TRUE
  211. return FALSE
  212. if __name__ == '__main__':
  213. import sys
  214. def error(msg):
  215. print(msg, end=' ', file=sys.stderr)
  216. print("usage: %s [number_of_loops]" % sys.argv[0], file=sys.stderr)
  217. sys.exit(100)
  218. nargs = len(sys.argv) - 1
  219. if nargs > 1:
  220. error("%d arguments are too many;" % nargs)
  221. elif nargs == 1:
  222. try: loops = int(sys.argv[1])
  223. except ValueError:
  224. error("Invalid argument %r;" % sys.argv[1])
  225. else:
  226. loops = LOOPS
  227. main(loops)