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.

pystone.py 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 = 10000
  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():
  42. benchtime, stones = pystones()
  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. return benchtime, (loops / benchtime)
  104. def Proc1(PtrParIn):
  105. PtrParIn.PtrComp = NextRecord = PtrGlb.copy()
  106. PtrParIn.IntComp = 5
  107. NextRecord.IntComp = PtrParIn.IntComp
  108. NextRecord.PtrComp = PtrParIn.PtrComp
  109. NextRecord.PtrComp = Proc3(NextRecord.PtrComp)
  110. if NextRecord.Discr == Ident1:
  111. NextRecord.IntComp = 6
  112. NextRecord.EnumComp = Proc6(PtrParIn.EnumComp)
  113. NextRecord.PtrComp = PtrGlb.PtrComp
  114. NextRecord.IntComp = Proc7(NextRecord.IntComp, 10)
  115. else:
  116. PtrParIn = NextRecord.copy()
  117. NextRecord.PtrComp = None
  118. return PtrParIn
  119. def Proc2(IntParIO):
  120. IntLoc = IntParIO + 10
  121. while 1:
  122. if Char1Glob == 'A':
  123. IntLoc = IntLoc - 1
  124. IntParIO = IntLoc - IntGlob
  125. EnumLoc = Ident1
  126. if EnumLoc == Ident1:
  127. break
  128. return IntParIO
  129. def Proc3(PtrParOut):
  130. global IntGlob
  131. if PtrGlb is not None:
  132. PtrParOut = PtrGlb.PtrComp
  133. else:
  134. IntGlob = 100
  135. PtrGlb.IntComp = Proc7(10, IntGlob)
  136. return PtrParOut
  137. def Proc4():
  138. global Char2Glob
  139. BoolLoc = Char1Glob == 'A'
  140. BoolLoc = BoolLoc or BoolGlob
  141. Char2Glob = 'B'
  142. def Proc5():
  143. global Char1Glob
  144. global BoolGlob
  145. Char1Glob = 'A'
  146. BoolGlob = FALSE
  147. def Proc6(EnumParIn):
  148. EnumParOut = EnumParIn
  149. if not Func3(EnumParIn):
  150. EnumParOut = Ident4
  151. if EnumParIn == Ident1:
  152. EnumParOut = Ident1
  153. elif EnumParIn == Ident2:
  154. if IntGlob > 100:
  155. EnumParOut = Ident1
  156. else:
  157. EnumParOut = Ident4
  158. elif EnumParIn == Ident3:
  159. EnumParOut = Ident2
  160. elif EnumParIn == Ident4:
  161. pass
  162. elif EnumParIn == Ident5:
  163. EnumParOut = Ident3
  164. return EnumParOut
  165. def Proc7(IntParI1, IntParI2):
  166. IntLoc = IntParI1 + 2
  167. IntParOut = IntParI2 + IntLoc
  168. return IntParOut
  169. def Proc8(Array1Par, Array2Par, IntParI1, IntParI2):
  170. global IntGlob
  171. IntLoc = IntParI1 + 5
  172. Array1Par[IntLoc] = IntParI2
  173. Array1Par[IntLoc+1] = Array1Par[IntLoc]
  174. Array1Par[IntLoc+30] = IntLoc
  175. for IntIndex in range(IntLoc, IntLoc+2):
  176. Array2Par[IntLoc][IntIndex] = IntLoc
  177. Array2Par[IntLoc][IntLoc-1] = Array2Par[IntLoc][IntLoc-1] + 1
  178. Array2Par[IntLoc+20][IntLoc] = Array1Par[IntLoc]
  179. IntGlob = 5
  180. def Func1(CharPar1, CharPar2):
  181. CharLoc1 = CharPar1
  182. CharLoc2 = CharLoc1
  183. if CharLoc2 != CharPar2:
  184. return Ident1
  185. else:
  186. return Ident2
  187. def Func2(StrParI1, StrParI2):
  188. IntLoc = 1
  189. while IntLoc <= 1:
  190. if Func1(StrParI1[IntLoc], StrParI2[IntLoc+1]) == Ident1:
  191. CharLoc = 'A'
  192. IntLoc = IntLoc + 1
  193. if CharLoc >= 'W' and CharLoc <= 'Z':
  194. IntLoc = 7
  195. if CharLoc == 'X':
  196. return TRUE
  197. else:
  198. if StrParI1 > StrParI2:
  199. IntLoc = IntLoc + 7
  200. return TRUE
  201. else:
  202. return FALSE
  203. def Func3(EnumParIn):
  204. EnumLoc = EnumParIn
  205. if EnumLoc == Ident3: return TRUE
  206. return FALSE
  207. if __name__ == '__main__':
  208. main()