Debian Codegraph @

rpython/translator/test/rpystone.py

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