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