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