Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 2 | |
| 3 | """ |
| 4 | "PYSTONE" Benchmark Program |
| 5 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 6 | Version: Python/1.1 (corresponds to C/1.1 plus 2 Pystone fixes) |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 7 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 8 | Author: Reinhold P. Weicker, CACM Vol 27, No 10, 10/84 pg. 1013. |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 9 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 13 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 14 | Translated from C to Python by Guido van Rossum. |
Guido van Rossum | 04f2b45 | 1997-01-18 02:20:37 +0000 | [diff] [blame] | 15 | |
| 16 | Version History: |
| 17 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 18 | Version 1.1 corrects two bugs in version 1.0: |
Guido van Rossum | 04f2b45 | 1997-01-18 02:20:37 +0000 | [diff] [blame] | 19 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 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(). |
Guido van Rossum | 04f2b45 | 1997-01-18 02:20:37 +0000 | [diff] [blame] | 23 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 04f2b45 | 1997-01-18 02:20:37 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 33 | """ |
| 34 | |
Guido van Rossum | 0855dd8 | 2002-08-06 17:21:20 +0000 | [diff] [blame] | 35 | LOOPS = 50000 |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 36 | |
| 37 | from time import clock |
| 38 | |
Guido van Rossum | 04f2b45 | 1997-01-18 02:20:37 +0000 | [diff] [blame] | 39 | __version__ = "1.1" |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 40 | |
| 41 | [Ident1, Ident2, Ident3, Ident4, Ident5] = range(1, 6) |
| 42 | |
| 43 | class Record: |
| 44 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 52 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 53 | def copy(self): |
| 54 | return Record(self.PtrComp, self.Discr, self.EnumComp, |
| 55 | self.IntComp, self.StringComp) |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 56 | |
| 57 | TRUE = 1 |
| 58 | FALSE = 0 |
| 59 | |
Alex Martelli | b993b06 | 2004-01-02 17:11:54 +0000 | [diff] [blame] | 60 | def main(loops=LOOPS): |
| 61 | benchtime, stones = pystones(loops) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 62 | print("Pystone(%s) time for %d passes = %g" % \ |
| 63 | (__version__, loops, benchtime)) |
| 64 | print("This machine benchmarks at %g pystones/second" % stones) |
Guido van Rossum | c1189eb | 1997-11-06 15:45:05 +0000 | [diff] [blame] | 65 | |
| 66 | |
| 67 | def pystones(loops=LOOPS): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 68 | return Proc0(loops) |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 69 | |
| 70 | IntGlob = 0 |
| 71 | BoolGlob = FALSE |
| 72 | Char1Glob = '\0' |
| 73 | Char2Glob = '\0' |
| 74 | Array1Glob = [0]*51 |
Georg Brandl | cbd2ab1 | 2010-12-04 10:39:14 +0000 | [diff] [blame] | 75 | Array2Glob = [x[:] for x in [Array1Glob]*51] |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 76 | PtrGlb = None |
| 77 | PtrGlbNext = None |
| 78 | |
Guido van Rossum | c1189eb | 1997-11-06 15:45:05 +0000 | [diff] [blame] | 79 | def Proc0(loops=LOOPS): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 80 | global IntGlob |
| 81 | global BoolGlob |
| 82 | global Char1Glob |
| 83 | global Char2Glob |
| 84 | global Array1Glob |
| 85 | global Array2Glob |
| 86 | global PtrGlb |
| 87 | global PtrGlbNext |
| 88 | |
| 89 | starttime = clock() |
| 90 | for i in range(loops): |
| 91 | pass |
| 92 | nulltime = clock() - starttime |
| 93 | |
| 94 | PtrGlbNext = Record() |
| 95 | PtrGlb = Record() |
| 96 | PtrGlb.PtrComp = PtrGlbNext |
| 97 | PtrGlb.Discr = Ident1 |
| 98 | PtrGlb.EnumComp = Ident3 |
| 99 | PtrGlb.IntComp = 40 |
| 100 | PtrGlb.StringComp = "DHRYSTONE PROGRAM, SOME STRING" |
| 101 | String1Loc = "DHRYSTONE PROGRAM, 1'ST STRING" |
| 102 | Array2Glob[8][7] = 10 |
| 103 | |
| 104 | starttime = clock() |
| 105 | |
| 106 | for i in range(loops): |
| 107 | Proc5() |
| 108 | Proc4() |
| 109 | IntLoc1 = 2 |
| 110 | IntLoc2 = 3 |
| 111 | String2Loc = "DHRYSTONE PROGRAM, 2'ND STRING" |
| 112 | EnumLoc = Ident2 |
| 113 | BoolGlob = not Func2(String1Loc, String2Loc) |
| 114 | while IntLoc1 < IntLoc2: |
| 115 | IntLoc3 = 5 * IntLoc1 - IntLoc2 |
| 116 | IntLoc3 = Proc7(IntLoc1, IntLoc2) |
| 117 | IntLoc1 = IntLoc1 + 1 |
| 118 | Proc8(Array1Glob, Array2Glob, IntLoc1, IntLoc3) |
| 119 | PtrGlb = Proc1(PtrGlb) |
| 120 | CharIndex = 'A' |
| 121 | while CharIndex <= Char2Glob: |
| 122 | if EnumLoc == Func1(CharIndex, 'C'): |
| 123 | EnumLoc = Proc6(Ident1) |
| 124 | CharIndex = chr(ord(CharIndex)+1) |
| 125 | IntLoc3 = IntLoc2 * IntLoc1 |
| 126 | IntLoc2 = IntLoc3 / IntLoc1 |
| 127 | IntLoc2 = 7 * (IntLoc3 - IntLoc2) - IntLoc1 |
| 128 | IntLoc1 = Proc2(IntLoc1) |
| 129 | |
| 130 | benchtime = clock() - starttime - nulltime |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 131 | if benchtime == 0.0: |
| 132 | loopsPerBenchtime = 0.0 |
| 133 | else: |
| 134 | loopsPerBenchtime = (loops / benchtime) |
| 135 | return benchtime, loopsPerBenchtime |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 136 | |
| 137 | def Proc1(PtrParIn): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 138 | PtrParIn.PtrComp = NextRecord = PtrGlb.copy() |
| 139 | PtrParIn.IntComp = 5 |
| 140 | NextRecord.IntComp = PtrParIn.IntComp |
| 141 | NextRecord.PtrComp = PtrParIn.PtrComp |
| 142 | NextRecord.PtrComp = Proc3(NextRecord.PtrComp) |
| 143 | if NextRecord.Discr == Ident1: |
| 144 | NextRecord.IntComp = 6 |
| 145 | NextRecord.EnumComp = Proc6(PtrParIn.EnumComp) |
| 146 | NextRecord.PtrComp = PtrGlb.PtrComp |
| 147 | NextRecord.IntComp = Proc7(NextRecord.IntComp, 10) |
| 148 | else: |
| 149 | PtrParIn = NextRecord.copy() |
| 150 | NextRecord.PtrComp = None |
| 151 | return PtrParIn |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 152 | |
| 153 | def Proc2(IntParIO): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 154 | IntLoc = IntParIO + 10 |
| 155 | while 1: |
| 156 | if Char1Glob == 'A': |
| 157 | IntLoc = IntLoc - 1 |
| 158 | IntParIO = IntLoc - IntGlob |
| 159 | EnumLoc = Ident1 |
| 160 | if EnumLoc == Ident1: |
| 161 | break |
| 162 | return IntParIO |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 163 | |
| 164 | def Proc3(PtrParOut): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 165 | global IntGlob |
| 166 | |
| 167 | if PtrGlb is not None: |
| 168 | PtrParOut = PtrGlb.PtrComp |
| 169 | else: |
| 170 | IntGlob = 100 |
| 171 | PtrGlb.IntComp = Proc7(10, IntGlob) |
| 172 | return PtrParOut |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 173 | |
| 174 | def Proc4(): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 175 | global Char2Glob |
| 176 | |
| 177 | BoolLoc = Char1Glob == 'A' |
| 178 | BoolLoc = BoolLoc or BoolGlob |
| 179 | Char2Glob = 'B' |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 180 | |
| 181 | def Proc5(): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 182 | global Char1Glob |
| 183 | global BoolGlob |
| 184 | |
| 185 | Char1Glob = 'A' |
| 186 | BoolGlob = FALSE |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 187 | |
| 188 | def Proc6(EnumParIn): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 189 | EnumParOut = EnumParIn |
| 190 | if not Func3(EnumParIn): |
| 191 | EnumParOut = Ident4 |
| 192 | if EnumParIn == Ident1: |
| 193 | EnumParOut = Ident1 |
| 194 | elif EnumParIn == Ident2: |
| 195 | if IntGlob > 100: |
| 196 | EnumParOut = Ident1 |
| 197 | else: |
| 198 | EnumParOut = Ident4 |
| 199 | elif EnumParIn == Ident3: |
| 200 | EnumParOut = Ident2 |
| 201 | elif EnumParIn == Ident4: |
| 202 | pass |
| 203 | elif EnumParIn == Ident5: |
| 204 | EnumParOut = Ident3 |
| 205 | return EnumParOut |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 206 | |
| 207 | def Proc7(IntParI1, IntParI2): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 208 | IntLoc = IntParI1 + 2 |
| 209 | IntParOut = IntParI2 + IntLoc |
| 210 | return IntParOut |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 211 | |
| 212 | def Proc8(Array1Par, Array2Par, IntParI1, IntParI2): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 213 | global IntGlob |
| 214 | |
| 215 | IntLoc = IntParI1 + 5 |
| 216 | Array1Par[IntLoc] = IntParI2 |
| 217 | Array1Par[IntLoc+1] = Array1Par[IntLoc] |
| 218 | Array1Par[IntLoc+30] = IntLoc |
| 219 | for IntIndex in range(IntLoc, IntLoc+2): |
| 220 | Array2Par[IntLoc][IntIndex] = IntLoc |
| 221 | Array2Par[IntLoc][IntLoc-1] = Array2Par[IntLoc][IntLoc-1] + 1 |
| 222 | Array2Par[IntLoc+20][IntLoc] = Array1Par[IntLoc] |
| 223 | IntGlob = 5 |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 224 | |
| 225 | def Func1(CharPar1, CharPar2): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 226 | CharLoc1 = CharPar1 |
| 227 | CharLoc2 = CharLoc1 |
| 228 | if CharLoc2 != CharPar2: |
| 229 | return Ident1 |
| 230 | else: |
| 231 | return Ident2 |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 232 | |
| 233 | def Func2(StrParI1, StrParI2): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 234 | IntLoc = 1 |
| 235 | while IntLoc <= 1: |
| 236 | if Func1(StrParI1[IntLoc], StrParI2[IntLoc+1]) == Ident1: |
| 237 | CharLoc = 'A' |
| 238 | IntLoc = IntLoc + 1 |
| 239 | if CharLoc >= 'W' and CharLoc <= 'Z': |
| 240 | IntLoc = 7 |
| 241 | if CharLoc == 'X': |
| 242 | return TRUE |
| 243 | else: |
| 244 | if StrParI1 > StrParI2: |
| 245 | IntLoc = IntLoc + 7 |
| 246 | return TRUE |
| 247 | else: |
| 248 | return FALSE |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 249 | |
| 250 | def Func3(EnumParIn): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 251 | EnumLoc = EnumParIn |
| 252 | if EnumLoc == Ident3: return TRUE |
| 253 | return FALSE |
Guido van Rossum | 189f8fb | 1995-03-30 09:42:43 +0000 | [diff] [blame] | 254 | |
| 255 | if __name__ == '__main__': |
Alex Martelli | b993b06 | 2004-01-02 17:11:54 +0000 | [diff] [blame] | 256 | import sys |
| 257 | def error(msg): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 258 | print(msg, end=' ', file=sys.stderr) |
| 259 | print("usage: %s [number_of_loops]" % sys.argv[0], file=sys.stderr) |
Alex Martelli | b993b06 | 2004-01-02 17:11:54 +0000 | [diff] [blame] | 260 | sys.exit(100) |
| 261 | nargs = len(sys.argv) - 1 |
| 262 | if nargs > 1: |
| 263 | error("%d arguments are too many;" % nargs) |
| 264 | elif nargs == 1: |
| 265 | try: loops = int(sys.argv[1]) |
| 266 | except ValueError: |
| 267 | error("Invalid argument %r;" % sys.argv[1]) |
| 268 | else: |
| 269 | loops = LOOPS |
| 270 | main(loops) |