Debian Codegraph @

benchmarks/pystone.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 """
4 "PYSTONE" Benchmark Program
5
6 Version: Python/1.1 (corresponds to C/1.1 plus 2 Pystone fixes)
7
8 Author: Reinhold P. Weicker, CACM Vol 27, No 10, 10/84 pg. 1013.
9
10 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.
13
14 Translated from C to Python by Guido van Rossum.
15
16 Version History:
17
18 Version 1.1 corrects two bugs in version 1.0:
19
20 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().
23
24 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.
32
33 """
34
35 LOOPS = 500000
36
37 import time # from time import clock
38
39 __version__ = "1.1"
40
41 [Ident1, Ident2, Ident3, Ident4, Ident5] = range(1, 6)
42
43
44 class Record:
45
46 def __init__(self, PtrComp=None, Discr=0, EnumComp=0, IntComp=0, StringComp=0):
47 self.PtrComp = PtrComp
48 self.Discr = Discr
49 self.EnumComp = EnumComp
50 self.IntComp = IntComp
51 self.StringComp = StringComp
52
53 def copy(self):
54 return Record(
55 self.PtrComp, self.Discr, self.EnumComp, self.IntComp, self.StringComp
56 )
57
58
59 TRUE = 1
60 FALSE = 0
61
62
63 def main(loops=LOOPS):
64 benchtime, stones = pystones(loops)
65 print("Pystone(%s) time for %d passes = %g" % (__version__, loops, benchtime))
66 print("This machine benchmarks at %g pystones/second" % stones)
67
68
69 def pystones(loops=LOOPS):
70 return Proc0(loops)
71
72
73 IntGlob = 0
74 BoolGlob = FALSE
75 Char1Glob = "\0"
76 Char2Glob = "\0"
77 Array1Glob = [0] * 51
78 Array2Glob = [x[:] for x in [Array1Glob] * 51]
79 PtrGlb = None
80 PtrGlbNext = None
81
82
83 def Proc0(loops=LOOPS):
84 global IntGlob
85 global BoolGlob
86 global Char1Glob
87 global Char2Glob
88 global Array1Glob
89 global Array2Glob
90 global PtrGlb
91 global PtrGlbNext
92
93 starttime = time.perf_counter()
94 for i in range(loops):
95 pass
96 nulltime = time.perf_counter() - starttime
97
98 PtrGlbNext = Record()
99 PtrGlb = Record()
100 PtrGlb.PtrComp = PtrGlbNext
101 PtrGlb.Discr = Ident1
102 PtrGlb.EnumComp = Ident3
103 PtrGlb.IntComp = 40
104 PtrGlb.StringComp = "DHRYSTONE PROGRAM, SOME STRING"
105 String1Loc = "DHRYSTONE PROGRAM, 1'ST STRING"
106 Array2Glob[8][7] = 10
107
108 starttime = time.perf_counter()
109
110 for i in range(loops):
111 Proc5()
112 Proc4()
113 IntLoc1 = 2
114 IntLoc2 = 3
115 String2Loc = "DHRYSTONE PROGRAM, 2'ND STRING"
116 EnumLoc = Ident2
117 BoolGlob = not Func2(String1Loc, String2Loc)
118 while IntLoc1 < IntLoc2:
119 IntLoc3 = 5 * IntLoc1 - IntLoc2
120 IntLoc3 = Proc7(IntLoc1, IntLoc2)
121 IntLoc1 = IntLoc1 + 1
122 Proc8(Array1Glob, Array2Glob, IntLoc1, IntLoc3)
123 PtrGlb = Proc1(PtrGlb)
124 CharIndex = "A"
125 while CharIndex <= Char2Glob:
126 if EnumLoc == Func1(CharIndex, "C"):
127 EnumLoc = Proc6(Ident1)
128 CharIndex = chr(ord(CharIndex) + 1)
129 IntLoc3 = IntLoc2 * IntLoc1
130 IntLoc2 = IntLoc3 / IntLoc1
131 IntLoc2 = 7 * (IntLoc3 - IntLoc2) - IntLoc1
132 IntLoc1 = Proc2(IntLoc1)
133
134 benchtime = time.perf_counter() - starttime - nulltime
135 if benchtime == 0.0:
136 loopsPerBenchtime = 0.0
137 else:
138 loopsPerBenchtime = loops / benchtime
139 return benchtime, loopsPerBenchtime
140
141
142 def Proc1(PtrParIn):
143 PtrParIn.PtrComp = NextRecord = PtrGlb.copy()
144 PtrParIn.IntComp = 5
145 NextRecord.IntComp = PtrParIn.IntComp
146 NextRecord.PtrComp = PtrParIn.PtrComp
147 NextRecord.PtrComp = Proc3(NextRecord.PtrComp)
148 if NextRecord.Discr == Ident1:
149 NextRecord.IntComp = 6
150 NextRecord.EnumComp = Proc6(PtrParIn.EnumComp)
151 NextRecord.PtrComp = PtrGlb.PtrComp
152 NextRecord.IntComp = Proc7(NextRecord.IntComp, 10)
153 else:
154 PtrParIn = NextRecord.copy()
155 NextRecord.PtrComp = None
156 return PtrParIn
157
158
159 def Proc2(IntParIO):
160 IntLoc = IntParIO + 10
161 while 1:
162 if Char1Glob == "A":
163 IntLoc = IntLoc - 1
164 IntParIO = IntLoc - IntGlob
165 EnumLoc = Ident1
166 if EnumLoc == Ident1:
167 break
168 return IntParIO
169
170
171 def Proc3(PtrParOut):
172 global IntGlob
173
174 if PtrGlb is not None:
175 PtrParOut = PtrGlb.PtrComp
176 else:
177 IntGlob = 100
178 PtrGlb.IntComp = Proc7(10, IntGlob)
179 return PtrParOut
180
181
182 def Proc4():
183 global Char2Glob
184
185 BoolLoc = Char1Glob == "A"
186 BoolLoc = BoolLoc or BoolGlob
187 Char2Glob = "B"
188
189
190 def Proc5():
191 global Char1Glob
192 global BoolGlob
193
194 Char1Glob = "A"
195 BoolGlob = FALSE
196
197
198 def Proc6(EnumParIn):
199 EnumParOut = EnumParIn
200 if not Func3(EnumParIn):
201 EnumParOut = Ident4
202 if EnumParIn == Ident1:
203 EnumParOut = Ident1
204 elif EnumParIn == Ident2:
205 if IntGlob > 100:
206 EnumParOut = Ident1
207 else:
208 EnumParOut = Ident4
209 elif EnumParIn == Ident3:
210 EnumParOut = Ident2
211 elif EnumParIn == Ident4:
212 pass
213 elif EnumParIn == Ident5:
214 EnumParOut = Ident3
215 return EnumParOut
216
217
218 def Proc7(IntParI1, IntParI2):
219 IntLoc = IntParI1 + 2
220 IntParOut = IntParI2 + IntLoc
221 return IntParOut
222
223
224 def Proc8(Array1Par, Array2Par, IntParI1, IntParI2):
225 global IntGlob
226
227 IntLoc = IntParI1 + 5
228 Array1Par[IntLoc] = IntParI2
229 Array1Par[IntLoc + 1] = Array1Par[IntLoc]
230 Array1Par[IntLoc + 30] = IntLoc
231 for IntIndex in range(IntLoc, IntLoc + 2):
232 Array2Par[IntLoc][IntIndex] = IntLoc
233 Array2Par[IntLoc][IntLoc - 1] = Array2Par[IntLoc][IntLoc - 1] + 1
234 Array2Par[IntLoc + 20][IntLoc] = Array1Par[IntLoc]
235 IntGlob = 5
236
237
238 def Func1(CharPar1, CharPar2):
239 CharLoc1 = CharPar1
240 CharLoc2 = CharLoc1
241 if CharLoc2 != CharPar2:
242 return Ident1
243 else:
244 return Ident2
245
246
247 def Func2(StrParI1, StrParI2):
248 IntLoc = 1
249 while IntLoc <= 1:
250 if Func1(StrParI1[IntLoc], StrParI2[IntLoc + 1]) == Ident1:
251 CharLoc = "A"
252 IntLoc = IntLoc + 1
253 if CharLoc >= "W" and CharLoc <= "Z":
254 IntLoc = 7
255 if CharLoc == "X":
256 return TRUE
257 else:
258 if StrParI1 > StrParI2:
259 IntLoc = IntLoc + 7
260 return TRUE
261 else:
262 return FALSE
263
264
265 def Func3(EnumParIn):
266 EnumLoc = EnumParIn
267 if EnumLoc == Ident3:
268 return TRUE
269 return FALSE
270
271
272 if __name__ == "__main__":
273 import sys
274
275 def error(msg):
276 print(msg, end=" ", file=sys.stderr)
277 print("usage: %s [number_of_loops]" % sys.argv[0], file=sys.stderr)
278 sys.exit(100)
279
280 nargs = len(sys.argv) - 1
281 if nargs > 1:
282 error("%d arguments are too many;" % nargs)
283 elif nargs == 1:
284 try:
285 loops = int(sys.argv[1])
286 except ValueError:
287 error("Invalid argument %r;" % sys.argv[1])
288 else:
289 loops = LOOPS
290 main(loops)