|
| 1 | +#! /usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +"PYSTONE" Benchmark Program |
| 6 | +
|
| 7 | +Version: Python/1.1 (corresponds to C/1.1 plus 2 Pystone fixes) |
| 8 | +
|
| 9 | +Author: Reinhold P. Weicker, CACM Vol 27, No 10, 10/84 pg. 1013. |
| 10 | +
|
| 11 | + Translated from ADA to C by Rick Richardson. |
| 12 | + Every method to preserve ADA-likeness has been used, |
| 13 | + at the expense of C-ness. |
| 14 | +
|
| 15 | + Translated from C to Python by Guido van Rossum. |
| 16 | +
|
| 17 | +Version History: |
| 18 | +
|
| 19 | + Inofficial version 1.1.1 by Chris Arndt: |
| 20 | +
|
| 21 | + - Make it run under Python 2 and 3 by using |
| 22 | + "from __future__ import print_function". |
| 23 | + - Change interpreter name in shebang line to plain |
| 24 | + "python". |
| 25 | + - Add source code encoding declaration. |
| 26 | +
|
| 27 | + Version 1.1 corrects two bugs in version 1.0: |
| 28 | +
|
| 29 | + First, it leaked memory: in Proc1(), NextRecord ends |
| 30 | + up having a pointer to itself. I have corrected this |
| 31 | + by zapping NextRecord.PtrComp at the end of Proc1(). |
| 32 | +
|
| 33 | + Second, Proc3() used the operator != to compare a |
| 34 | + record to None. This is rather inefficient and not |
| 35 | + true to the intention of the original benchmark (where |
| 36 | + a pointer comparison to None is intended; the != |
| 37 | + operator attempts to find a method __cmp__ to do value |
| 38 | + comparison of the record). Version 1.1 runs 5-10 |
| 39 | + percent faster than version 1.0, so benchmark figures |
| 40 | + of different versions can't be compared directly. |
| 41 | +
|
| 42 | +""" |
| 43 | + |
| 44 | +from __future__importprint_function |
| 45 | + |
| 46 | +LOOPS=50000 |
| 47 | + |
| 48 | +fromtimeimporttimeasclock |
| 49 | + |
| 50 | +__version__="1.1.1" |
| 51 | + |
| 52 | +[Ident1,Ident2,Ident3,Ident4,Ident5]=range(1,6) |
| 53 | + |
| 54 | +classRecord: |
| 55 | + |
| 56 | +def__init__(self,PtrComp=None,Discr=0,EnumComp=0, |
| 57 | +IntComp=0,StringComp=0): |
| 58 | +self.PtrComp=PtrComp |
| 59 | +self.Discr=Discr |
| 60 | +self.EnumComp=EnumComp |
| 61 | +self.IntComp=IntComp |
| 62 | +self.StringComp=StringComp |
| 63 | + |
| 64 | +defcopy(self): |
| 65 | +returnRecord(self.PtrComp,self.Discr,self.EnumComp, |
| 66 | +self.IntComp,self.StringComp) |
| 67 | + |
| 68 | +TRUE=1 |
| 69 | +FALSE=0 |
| 70 | + |
| 71 | +defmain(loops=LOOPS): |
| 72 | +benchtime,stones=pystones(loops) |
| 73 | +print("Pystone(%s) time for %d passes = %f"% \ |
| 74 | + (__version__,loops,benchtime)) |
| 75 | +print("This machine benchmarks at %f pystones/second"%stones) |
| 76 | + |
| 77 | + |
| 78 | +defpystones(loops=LOOPS): |
| 79 | +returnProc0(loops) |
| 80 | + |
| 81 | +IntGlob=0 |
| 82 | +BoolGlob=FALSE |
| 83 | +Char1Glob='\0' |
| 84 | +Char2Glob='\0' |
| 85 | +Array1Glob= [0]*51 |
| 86 | +Array2Glob= [x[:]forxin [Array1Glob]*51] |
| 87 | +PtrGlb=None |
| 88 | +PtrGlbNext=None |
| 89 | + |
| 90 | +defProc0(loops=LOOPS): |
| 91 | +globalIntGlob |
| 92 | +globalBoolGlob |
| 93 | +globalChar1Glob |
| 94 | +globalChar2Glob |
| 95 | +globalArray1Glob |
| 96 | +globalArray2Glob |
| 97 | +globalPtrGlb |
| 98 | +globalPtrGlbNext |
| 99 | + |
| 100 | +starttime=clock() |
| 101 | +foriinrange(loops): |
| 102 | +pass |
| 103 | +nulltime=clock()-starttime |
| 104 | + |
| 105 | +PtrGlbNext=Record() |
| 106 | +PtrGlb=Record() |
| 107 | +PtrGlb.PtrComp=PtrGlbNext |
| 108 | +PtrGlb.Discr=Ident1 |
| 109 | +PtrGlb.EnumComp=Ident3 |
| 110 | +PtrGlb.IntComp=40 |
| 111 | +PtrGlb.StringComp="DHRYSTONE PROGRAM, SOME STRING" |
| 112 | +String1Loc="DHRYSTONE PROGRAM, 1'ST STRING" |
| 113 | +Array2Glob[8][7]=10 |
| 114 | + |
| 115 | +starttime=clock() |
| 116 | + |
| 117 | +foriinrange(loops): |
| 118 | +Proc5() |
| 119 | +Proc4() |
| 120 | +IntLoc1=2 |
| 121 | +IntLoc2=3 |
| 122 | +String2Loc="DHRYSTONE PROGRAM, 2'ND STRING" |
| 123 | +EnumLoc=Ident2 |
| 124 | +BoolGlob=notFunc2(String1Loc,String2Loc) |
| 125 | +whileIntLoc1<IntLoc2: |
| 126 | +IntLoc3=5*IntLoc1-IntLoc2 |
| 127 | +IntLoc3=Proc7(IntLoc1,IntLoc2) |
| 128 | +IntLoc1=IntLoc1+1 |
| 129 | +Proc8(Array1Glob,Array2Glob,IntLoc1,IntLoc3) |
| 130 | +PtrGlb=Proc1(PtrGlb) |
| 131 | +CharIndex='A' |
| 132 | +whileCharIndex<=Char2Glob: |
| 133 | +ifEnumLoc==Func1(CharIndex,'C'): |
| 134 | +EnumLoc=Proc6(Ident1) |
| 135 | +CharIndex=chr(ord(CharIndex)+1) |
| 136 | +IntLoc3=IntLoc2*IntLoc1 |
| 137 | +IntLoc2=IntLoc3/IntLoc1 |
| 138 | +IntLoc2=7* (IntLoc3-IntLoc2)-IntLoc1 |
| 139 | +IntLoc1=Proc2(IntLoc1) |
| 140 | + |
| 141 | +benchtime=clock()-starttime-nulltime |
| 142 | +ifbenchtime==0.0: |
| 143 | +loopsPerBenchtime=0.0 |
| 144 | +else: |
| 145 | +loopsPerBenchtime= (loops/benchtime) |
| 146 | +returnbenchtime,loopsPerBenchtime |
| 147 | + |
| 148 | +defProc1(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 | +ifNextRecord.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 | +returnPtrParIn |
| 163 | + |
| 164 | +defProc2(IntParIO): |
| 165 | +IntLoc=IntParIO+10 |
| 166 | +while1: |
| 167 | +ifChar1Glob=='A': |
| 168 | +IntLoc=IntLoc-1 |
| 169 | +IntParIO=IntLoc-IntGlob |
| 170 | +EnumLoc=Ident1 |
| 171 | +ifEnumLoc==Ident1: |
| 172 | +break |
| 173 | +returnIntParIO |
| 174 | + |
| 175 | +defProc3(PtrParOut): |
| 176 | +globalIntGlob |
| 177 | + |
| 178 | +ifPtrGlbisnotNone: |
| 179 | +PtrParOut=PtrGlb.PtrComp |
| 180 | +else: |
| 181 | +IntGlob=100 |
| 182 | +PtrGlb.IntComp=Proc7(10,IntGlob) |
| 183 | +returnPtrParOut |
| 184 | + |
| 185 | +defProc4(): |
| 186 | +globalChar2Glob |
| 187 | + |
| 188 | +BoolLoc=Char1Glob=='A' |
| 189 | +BoolLoc=BoolLocorBoolGlob |
| 190 | +Char2Glob='B' |
| 191 | + |
| 192 | +defProc5(): |
| 193 | +globalChar1Glob |
| 194 | +globalBoolGlob |
| 195 | + |
| 196 | +Char1Glob='A' |
| 197 | +BoolGlob=FALSE |
| 198 | + |
| 199 | +defProc6(EnumParIn): |
| 200 | +EnumParOut=EnumParIn |
| 201 | +ifnotFunc3(EnumParIn): |
| 202 | +EnumParOut=Ident4 |
| 203 | +ifEnumParIn==Ident1: |
| 204 | +EnumParOut=Ident1 |
| 205 | +elifEnumParIn==Ident2: |
| 206 | +ifIntGlob>100: |
| 207 | +EnumParOut=Ident1 |
| 208 | +else: |
| 209 | +EnumParOut=Ident4 |
| 210 | +elifEnumParIn==Ident3: |
| 211 | +EnumParOut=Ident2 |
| 212 | +elifEnumParIn==Ident4: |
| 213 | +pass |
| 214 | +elifEnumParIn==Ident5: |
| 215 | +EnumParOut=Ident3 |
| 216 | +returnEnumParOut |
| 217 | + |
| 218 | +defProc7(IntParI1,IntParI2): |
| 219 | +IntLoc=IntParI1+2 |
| 220 | +IntParOut=IntParI2+IntLoc |
| 221 | +returnIntParOut |
| 222 | + |
| 223 | +defProc8(Array1Par,Array2Par,IntParI1,IntParI2): |
| 224 | +globalIntGlob |
| 225 | + |
| 226 | +IntLoc=IntParI1+5 |
| 227 | +Array1Par[IntLoc]=IntParI2 |
| 228 | +Array1Par[IntLoc+1]=Array1Par[IntLoc] |
| 229 | +Array1Par[IntLoc+30]=IntLoc |
| 230 | +forIntIndexinrange(IntLoc,IntLoc+2): |
| 231 | +Array2Par[IntLoc][IntIndex]=IntLoc |
| 232 | +Array2Par[IntLoc][IntLoc-1]=Array2Par[IntLoc][IntLoc-1]+1 |
| 233 | +Array2Par[IntLoc+20][IntLoc]=Array1Par[IntLoc] |
| 234 | +IntGlob=5 |
| 235 | + |
| 236 | +defFunc1(CharPar1,CharPar2): |
| 237 | +CharLoc1=CharPar1 |
| 238 | +CharLoc2=CharLoc1 |
| 239 | +ifCharLoc2!=CharPar2: |
| 240 | +returnIdent1 |
| 241 | +else: |
| 242 | +returnIdent2 |
| 243 | + |
| 244 | +defFunc2(StrParI1,StrParI2): |
| 245 | +IntLoc=1 |
| 246 | +whileIntLoc<=1: |
| 247 | +ifFunc1(StrParI1[IntLoc],StrParI2[IntLoc+1])==Ident1: |
| 248 | +CharLoc='A' |
| 249 | +IntLoc=IntLoc+1 |
| 250 | +ifCharLoc>='W'andCharLoc<='Z': |
| 251 | +IntLoc=7 |
| 252 | +ifCharLoc=='X': |
| 253 | +returnTRUE |
| 254 | +else: |
| 255 | +ifStrParI1>StrParI2: |
| 256 | +IntLoc=IntLoc+7 |
| 257 | +returnTRUE |
| 258 | +else: |
| 259 | +returnFALSE |
| 260 | + |
| 261 | +defFunc3(EnumParIn): |
| 262 | +EnumLoc=EnumParIn |
| 263 | +ifEnumLoc==Ident3:returnTRUE |
| 264 | +returnFALSE |
| 265 | + |
| 266 | +if__name__=='__main__': |
| 267 | +importsys |
| 268 | +deferror(msg): |
| 269 | +print(msg,end=' ',file=sys.stderr) |
| 270 | +print("usage: %s [number_of_loops]"%sys.argv[0],file=sys.stderr) |
| 271 | +sys.exit(100) |
| 272 | +nargs=len(sys.argv)-1 |
| 273 | +ifnargs>1: |
| 274 | +error("%d arguments are too many;"%nargs) |
| 275 | +elifnargs==1: |
| 276 | +try:loops=int(sys.argv[1]) |
| 277 | +exceptValueError: |
| 278 | +error("Invalid argument %r;"%sys.argv[1]) |
| 279 | +else: |
| 280 | +loops=LOOPS |
| 281 | +main(loops) |