blob: 23e3b7fbf00cf7151bdca078f0d7dd13b1607fd3 [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
Guido van Rossum04f2b451997-01-18 02:20:37 +00006Version: Python/1.1 (corresponds to C/1.1 plus 2 Pystone fixes)
Guido van Rossum189f8fb1995-03-30 09:42:43 +00007
8Author: 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.
Guido van Rossum04f2b451997-01-18 02:20:37 +000015
16Version 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
Guido van Rossum189f8fb1995-03-30 09:42:43 +000033"""
34
35LOOPS = 1000
36
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
45 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
52
53 def copy(self):
54 return Record(self.PtrComp, self.Discr, self.EnumComp,
55 self.IntComp, self.StringComp)
56
57TRUE = 1
58FALSE = 0
59
60def main():
61 Proc0()
62
63IntGlob = 0
64BoolGlob = FALSE
65Char1Glob = '\0'
66Char2Glob = '\0'
67Array1Glob = [0]*51
68Array2Glob = map(lambda x: x[:], [Array1Glob]*51)
69PtrGlb = None
70PtrGlbNext = None
71
72def Proc0():
73 global IntGlob
74 global BoolGlob
75 global Char1Glob
76 global Char2Glob
77 global Array1Glob
78 global Array2Glob
79 global PtrGlb
80 global PtrGlbNext
81
82 starttime = clock()
83 for i in range(LOOPS):
84 pass
85 nulltime = clock() - starttime
86
87 PtrGlbNext = Record()
88 PtrGlb = Record()
89 PtrGlb.PtrComp = PtrGlbNext
90 PtrGlb.Discr = Ident1
91 PtrGlb.EnumComp = Ident3
92 PtrGlb.IntComp = 40
93 PtrGlb.StringComp = "DHRYSTONE PROGRAM, SOME STRING"
94 String1Loc = "DHRYSTONE PROGRAM, 1'ST STRING"
95 Array2Glob[8][7] = 10
96
97 starttime = clock()
98
99 for i in range(LOOPS):
100 Proc5()
101 Proc4()
102 IntLoc1 = 2
103 IntLoc2 = 3
104 String2Loc = "DHRYSTONE PROGRAM, 2'ND STRING"
105 EnumLoc = Ident2
106 BoolGlob = not Func2(String1Loc, String2Loc)
107 while IntLoc1 < IntLoc2:
108 IntLoc3 = 5 * IntLoc1 - IntLoc2
109 IntLoc3 = Proc7(IntLoc1, IntLoc2)
110 IntLoc1 = IntLoc1 + 1
111 Proc8(Array1Glob, Array2Glob, IntLoc1, IntLoc3)
112 PtrGlb = Proc1(PtrGlb)
113 CharIndex = 'A'
114 while CharIndex <= Char2Glob:
115 if EnumLoc == Func1(CharIndex, 'C'):
116 EnumLoc = Proc6(Ident1)
117 CharIndex = chr(ord(CharIndex)+1)
118 IntLoc3 = IntLoc2 * IntLoc1
119 IntLoc2 = IntLoc3 / IntLoc1
120 IntLoc2 = 7 * (IntLoc3 - IntLoc2) - IntLoc1
121 IntLoc1 = Proc2(IntLoc1)
122
123 benchtime = clock() - starttime - nulltime
124 print "Pystone(%s) time for %d passes = %g" % \
125 (__version__, LOOPS, benchtime)
126 print "This machine benchmarks at %g pystones/second" % \
127 (LOOPS/benchtime)
128
129def Proc1(PtrParIn):
130 PtrParIn.PtrComp = NextRecord = PtrGlb.copy()
131 PtrParIn.IntComp = 5
132 NextRecord.IntComp = PtrParIn.IntComp
133 NextRecord.PtrComp = PtrParIn.PtrComp
134 NextRecord.PtrComp = Proc3(NextRecord.PtrComp)
135 if NextRecord.Discr == Ident1:
136 NextRecord.IntComp = 6
137 NextRecord.EnumComp = Proc6(PtrParIn.EnumComp)
138 NextRecord.PtrComp = PtrGlb.PtrComp
139 NextRecord.IntComp = Proc7(NextRecord.IntComp, 10)
140 else:
141 PtrParIn = NextRecord.copy()
Guido van Rossum04f2b451997-01-18 02:20:37 +0000142 NextRecord.PtrComp = None
Guido van Rossum189f8fb1995-03-30 09:42:43 +0000143 return PtrParIn
144
145def Proc2(IntParIO):
146 IntLoc = IntParIO + 10
147 while 1:
148 if Char1Glob == 'A':
149 IntLoc = IntLoc - 1
150 IntParIO = IntLoc - IntGlob
151 EnumLoc = Ident1
152 if EnumLoc == Ident1:
153 break
154 return IntParIO
155
156def Proc3(PtrParOut):
157 global IntGlob
158
Guido van Rossum04f2b451997-01-18 02:20:37 +0000159 if PtrGlb is not None:
Guido van Rossum189f8fb1995-03-30 09:42:43 +0000160 PtrParOut = PtrGlb.PtrComp
161 else:
162 IntGlob = 100
163 PtrGlb.IntComp = Proc7(10, IntGlob)
164 return PtrParOut
165
166def Proc4():
167 global Char2Glob
168
169 BoolLoc = Char1Glob == 'A'
170 BoolLoc = BoolLoc or BoolGlob
171 Char2Glob = 'B'
172
173def Proc5():
174 global Char1Glob
175 global BoolGlob
176
177 Char1Glob = 'A'
178 BoolGlob = FALSE
179
180def Proc6(EnumParIn):
181 EnumParOut = EnumParIn
182 if not Func3(EnumParIn):
183 EnumParOut = Ident4
184 if EnumParIn == Ident1:
185 EnumParOut = Ident1
186 elif EnumParIn == Ident2:
187 if IntGlob > 100:
188 EnumParOut = Ident1
189 else:
190 EnumParOut = Ident4
191 elif EnumParIn == Ident3:
192 EnumParOut = Ident2
193 elif EnumParIn == Ident4:
194 pass
195 elif EnumParIn == Ident5:
196 EnumParOut = Ident3
197 return EnumParOut
198
199def Proc7(IntParI1, IntParI2):
200 IntLoc = IntParI1 + 2
201 IntParOut = IntParI2 + IntLoc
202 return IntParOut
203
204def Proc8(Array1Par, Array2Par, IntParI1, IntParI2):
205 global IntGlob
206
207 IntLoc = IntParI1 + 5
208 Array1Par[IntLoc] = IntParI2
209 Array1Par[IntLoc+1] = Array1Par[IntLoc]
210 Array1Par[IntLoc+30] = IntLoc
211 for IntIndex in range(IntLoc, IntLoc+2):
212 Array2Par[IntLoc][IntIndex] = IntLoc
213 Array2Par[IntLoc][IntLoc-1] = Array2Par[IntLoc][IntLoc-1] + 1
214 Array2Par[IntLoc+20][IntLoc] = Array1Par[IntLoc]
215 IntGlob = 5
216
217def Func1(CharPar1, CharPar2):
218 CharLoc1 = CharPar1
219 CharLoc2 = CharLoc1
220 if CharLoc2 != CharPar2:
221 return Ident1
222 else:
223 return Ident2
224
225def Func2(StrParI1, StrParI2):
226 IntLoc = 1
227 while IntLoc <= 1:
228 if Func1(StrParI1[IntLoc], StrParI2[IntLoc+1]) == Ident1:
229 CharLoc = 'A'
230 IntLoc = IntLoc + 1
231 if CharLoc >= 'W' and CharLoc <= 'Z':
232 IntLoc = 7
233 if CharLoc == 'X':
234 return TRUE
235 else:
236 if StrParI1 > StrParI2:
237 IntLoc = IntLoc + 7
238 return TRUE
239 else:
240 return FALSE
241
242def Func3(EnumParIn):
243 EnumLoc = EnumParIn
244 if EnumLoc == Ident3: return TRUE
245 return FALSE
246
247if __name__ == '__main__':
248 main()