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