blob: 6c04b55714696a0380ecdebed87954d01118a34c [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossum189f8fb1995-03-30 09:42:43 +00002
3"""
4"PYSTONE" Benchmark Program
5
Fred Drake004d5e62000-10-23 17:22:08 +00006Version: Python/1.1 (corresponds to C/1.1 plus 2 Pystone fixes)
Guido van Rossum189f8fb1995-03-30 09:42:43 +00007
Fred Drake004d5e62000-10-23 17:22:08 +00008Author: Reinhold P. Weicker, CACM Vol 27, No 10, 10/84 pg. 1013.
Guido van Rossum189f8fb1995-03-30 09:42:43 +00009
Fred Drake004d5e62000-10-23 17:22:08 +000010 Translated from ADA to C by Rick Richardson.
11 Every method to preserve ADA-likeness has been used,
12 at the expense of C-ness.
Guido van Rossum189f8fb1995-03-30 09:42:43 +000013
Fred Drake004d5e62000-10-23 17:22:08 +000014 Translated from C to Python by Guido van Rossum.
Guido van Rossum04f2b451997-01-18 02:20:37 +000015
16Version History:
17
Fred Drake004d5e62000-10-23 17:22:08 +000018 Version 1.1 corrects two bugs in version 1.0:
Guido van Rossum04f2b451997-01-18 02:20:37 +000019
Fred Drake004d5e62000-10-23 17:22:08 +000020 First, it leaked memory: in Proc1(), NextRecord ends
21 up having a pointer to itself. I have corrected this
22 by zapping NextRecord.PtrComp at the end of Proc1().
Guido van Rossum04f2b451997-01-18 02:20:37 +000023
Fred Drake004d5e62000-10-23 17:22:08 +000024 Second, Proc3() used the operator != to compare a
25 record to None. This is rather inefficient and not
26 true to the intention of the original benchmark (where
27 a pointer comparison to None is intended; the !=
28 operator attempts to find a method __cmp__ to do value
29 comparison of the record). Version 1.1 runs 5-10
30 percent faster than version 1.0, so benchmark figures
31 of different versions can't be compared directly.
Guido van Rossum04f2b451997-01-18 02:20:37 +000032
Guido van Rossum189f8fb1995-03-30 09:42:43 +000033"""
34
Guido van Rossum5526e391997-08-14 20:14:12 +000035LOOPS = 10000
Guido van Rossum189f8fb1995-03-30 09:42:43 +000036
37from time import clock
38
Guido van Rossum04f2b451997-01-18 02:20:37 +000039__version__ = "1.1"
Guido van Rossum189f8fb1995-03-30 09:42:43 +000040
41[Ident1, Ident2, Ident3, Ident4, Ident5] = range(1, 6)
42
43class Record:
44
Fred Drake004d5e62000-10-23 17:22:08 +000045 def __init__(self, PtrComp = None, Discr = 0, EnumComp = 0,
46 IntComp = 0, StringComp = 0):
47 self.PtrComp = PtrComp
48 self.Discr = Discr
49 self.EnumComp = EnumComp
50 self.IntComp = IntComp
51 self.StringComp = StringComp
Guido van Rossum189f8fb1995-03-30 09:42:43 +000052
Fred Drake004d5e62000-10-23 17:22:08 +000053 def copy(self):
54 return Record(self.PtrComp, self.Discr, self.EnumComp,
55 self.IntComp, self.StringComp)
Guido van Rossum189f8fb1995-03-30 09:42:43 +000056
57TRUE = 1
58FALSE = 0
59
60def main():
Fred Drake004d5e62000-10-23 17:22:08 +000061 benchtime, stones = pystones()
62 print "Pystone(%s) time for %d passes = %g" % \
63 (__version__, LOOPS, benchtime)
64 print "This machine benchmarks at %g pystones/second" % stones
Guido van Rossumc1189eb1997-11-06 15:45:05 +000065
66
67def pystones(loops=LOOPS):
Fred Drake004d5e62000-10-23 17:22:08 +000068 return Proc0(loops)
Guido van Rossum189f8fb1995-03-30 09:42:43 +000069
70IntGlob = 0
71BoolGlob = FALSE
72Char1Glob = '\0'
73Char2Glob = '\0'
74Array1Glob = [0]*51
75Array2Glob = map(lambda x: x[:], [Array1Glob]*51)
76PtrGlb = None
77PtrGlbNext = None
78
Guido van Rossumc1189eb1997-11-06 15:45:05 +000079def Proc0(loops=LOOPS):
Fred Drake004d5e62000-10-23 17:22:08 +000080 global IntGlob
81 global BoolGlob
82 global Char1Glob
83 global Char2Glob
84 global Array1Glob
85 global Array2Glob
86 global PtrGlb
87 global PtrGlbNext
88
89 starttime = clock()
90 for i in range(loops):
91 pass
92 nulltime = clock() - starttime
93
94 PtrGlbNext = Record()
95 PtrGlb = Record()
96 PtrGlb.PtrComp = PtrGlbNext
97 PtrGlb.Discr = Ident1
98 PtrGlb.EnumComp = Ident3
99 PtrGlb.IntComp = 40
100 PtrGlb.StringComp = "DHRYSTONE PROGRAM, SOME STRING"
101 String1Loc = "DHRYSTONE PROGRAM, 1'ST STRING"
102 Array2Glob[8][7] = 10
103
104 starttime = clock()
105
106 for i in range(loops):
107 Proc5()
108 Proc4()
109 IntLoc1 = 2
110 IntLoc2 = 3
111 String2Loc = "DHRYSTONE PROGRAM, 2'ND STRING"
112 EnumLoc = Ident2
113 BoolGlob = not Func2(String1Loc, String2Loc)
114 while IntLoc1 < IntLoc2:
115 IntLoc3 = 5 * IntLoc1 - IntLoc2
116 IntLoc3 = Proc7(IntLoc1, IntLoc2)
117 IntLoc1 = IntLoc1 + 1
118 Proc8(Array1Glob, Array2Glob, IntLoc1, IntLoc3)
119 PtrGlb = Proc1(PtrGlb)
120 CharIndex = 'A'
121 while CharIndex <= Char2Glob:
122 if EnumLoc == Func1(CharIndex, 'C'):
123 EnumLoc = Proc6(Ident1)
124 CharIndex = chr(ord(CharIndex)+1)
125 IntLoc3 = IntLoc2 * IntLoc1
126 IntLoc2 = IntLoc3 / IntLoc1
127 IntLoc2 = 7 * (IntLoc3 - IntLoc2) - IntLoc1
128 IntLoc1 = Proc2(IntLoc1)
129
130 benchtime = clock() - starttime - nulltime
131 return benchtime, (loops / benchtime)
Guido van Rossum189f8fb1995-03-30 09:42:43 +0000132
133def Proc1(PtrParIn):
Fred Drake004d5e62000-10-23 17:22:08 +0000134 PtrParIn.PtrComp = NextRecord = PtrGlb.copy()
135 PtrParIn.IntComp = 5
136 NextRecord.IntComp = PtrParIn.IntComp
137 NextRecord.PtrComp = PtrParIn.PtrComp
138 NextRecord.PtrComp = Proc3(NextRecord.PtrComp)
139 if NextRecord.Discr == Ident1:
140 NextRecord.IntComp = 6
141 NextRecord.EnumComp = Proc6(PtrParIn.EnumComp)
142 NextRecord.PtrComp = PtrGlb.PtrComp
143 NextRecord.IntComp = Proc7(NextRecord.IntComp, 10)
144 else:
145 PtrParIn = NextRecord.copy()
146 NextRecord.PtrComp = None
147 return PtrParIn
Guido van Rossum189f8fb1995-03-30 09:42:43 +0000148
149def Proc2(IntParIO):
Fred Drake004d5e62000-10-23 17:22:08 +0000150 IntLoc = IntParIO + 10
151 while 1:
152 if Char1Glob == 'A':
153 IntLoc = IntLoc - 1
154 IntParIO = IntLoc - IntGlob
155 EnumLoc = Ident1
156 if EnumLoc == Ident1:
157 break
158 return IntParIO
Guido van Rossum189f8fb1995-03-30 09:42:43 +0000159
160def Proc3(PtrParOut):
Fred Drake004d5e62000-10-23 17:22:08 +0000161 global IntGlob
162
163 if PtrGlb is not None:
164 PtrParOut = PtrGlb.PtrComp
165 else:
166 IntGlob = 100
167 PtrGlb.IntComp = Proc7(10, IntGlob)
168 return PtrParOut
Guido van Rossum189f8fb1995-03-30 09:42:43 +0000169
170def Proc4():
Fred Drake004d5e62000-10-23 17:22:08 +0000171 global Char2Glob
172
173 BoolLoc = Char1Glob == 'A'
174 BoolLoc = BoolLoc or BoolGlob
175 Char2Glob = 'B'
Guido van Rossum189f8fb1995-03-30 09:42:43 +0000176
177def Proc5():
Fred Drake004d5e62000-10-23 17:22:08 +0000178 global Char1Glob
179 global BoolGlob
180
181 Char1Glob = 'A'
182 BoolGlob = FALSE
Guido van Rossum189f8fb1995-03-30 09:42:43 +0000183
184def Proc6(EnumParIn):
Fred Drake004d5e62000-10-23 17:22:08 +0000185 EnumParOut = EnumParIn
186 if not Func3(EnumParIn):
187 EnumParOut = Ident4
188 if EnumParIn == Ident1:
189 EnumParOut = Ident1
190 elif EnumParIn == Ident2:
191 if IntGlob > 100:
192 EnumParOut = Ident1
193 else:
194 EnumParOut = Ident4
195 elif EnumParIn == Ident3:
196 EnumParOut = Ident2
197 elif EnumParIn == Ident4:
198 pass
199 elif EnumParIn == Ident5:
200 EnumParOut = Ident3
201 return EnumParOut
Guido van Rossum189f8fb1995-03-30 09:42:43 +0000202
203def Proc7(IntParI1, IntParI2):
Fred Drake004d5e62000-10-23 17:22:08 +0000204 IntLoc = IntParI1 + 2
205 IntParOut = IntParI2 + IntLoc
206 return IntParOut
Guido van Rossum189f8fb1995-03-30 09:42:43 +0000207
208def Proc8(Array1Par, Array2Par, IntParI1, IntParI2):
Fred Drake004d5e62000-10-23 17:22:08 +0000209 global IntGlob
210
211 IntLoc = IntParI1 + 5
212 Array1Par[IntLoc] = IntParI2
213 Array1Par[IntLoc+1] = Array1Par[IntLoc]
214 Array1Par[IntLoc+30] = IntLoc
215 for IntIndex in range(IntLoc, IntLoc+2):
216 Array2Par[IntLoc][IntIndex] = IntLoc
217 Array2Par[IntLoc][IntLoc-1] = Array2Par[IntLoc][IntLoc-1] + 1
218 Array2Par[IntLoc+20][IntLoc] = Array1Par[IntLoc]
219 IntGlob = 5
Guido van Rossum189f8fb1995-03-30 09:42:43 +0000220
221def Func1(CharPar1, CharPar2):
Fred Drake004d5e62000-10-23 17:22:08 +0000222 CharLoc1 = CharPar1
223 CharLoc2 = CharLoc1
224 if CharLoc2 != CharPar2:
225 return Ident1
226 else:
227 return Ident2
Guido van Rossum189f8fb1995-03-30 09:42:43 +0000228
229def Func2(StrParI1, StrParI2):
Fred Drake004d5e62000-10-23 17:22:08 +0000230 IntLoc = 1
231 while IntLoc <= 1:
232 if Func1(StrParI1[IntLoc], StrParI2[IntLoc+1]) == Ident1:
233 CharLoc = 'A'
234 IntLoc = IntLoc + 1
235 if CharLoc >= 'W' and CharLoc <= 'Z':
236 IntLoc = 7
237 if CharLoc == 'X':
238 return TRUE
239 else:
240 if StrParI1 > StrParI2:
241 IntLoc = IntLoc + 7
242 return TRUE
243 else:
244 return FALSE
Guido van Rossum189f8fb1995-03-30 09:42:43 +0000245
246def Func3(EnumParIn):
Fred Drake004d5e62000-10-23 17:22:08 +0000247 EnumLoc = EnumParIn
248 if EnumLoc == Ident3: return TRUE
249 return FALSE
Guido van Rossum189f8fb1995-03-30 09:42:43 +0000250
251if __name__ == '__main__':
Fred Drake004d5e62000-10-23 17:22:08 +0000252 main()