0001 #! /usr/bin/env python 0002 0003 """ 0004 "PYSTONE" Benchmark Program 0005 0006 Version: Python/1.1 (corresponds to C/1.1 plus 2 Pystone fixes) 0007 0008 Author: Reinhold P. Weicker, CACM Vol 27, No 10, 10/84 pg. 1013. 0009 0010 Translated from ADA to C by Rick Richardson. 0011 Every method to preserve ADA-likeness has been used, 0012 at the expense of C-ness. 0013 0014 Translated from C to Python by Guido van Rossum. 0015 0016 Version History: 0017 0018 Version 1.1 corrects two bugs in version 1.0: 0019 0020 First, it leaked memory: in Proc1(), NextRecord ends 0021 up having a pointer to itself. I have corrected this 0022 by zapping NextRecord.PtrComp at the end of Proc1(). 0023 0024 Second, Proc3() used the operator != to compare a 0025 record to None. This is rather inefficient and not 0026 true to the intention of the original benchmark (where 0027 a pointer comparison to None is intended; the != 0028 operator attempts to find a method __cmp__ to do value 0029 comparison of the record). Version 1.1 runs 5-10 0030 percent faster than version 1.0, so benchmark figures 0031 of different versions can't be compared directly. 0032 0033 """ 0034 0035 LOOPS = 50000 0036 0037 from time import clock 0038 0039 __version__ = "1.1" 0040 0041 [Ident1, Ident2, Ident3, Ident4, Ident5] = range(1, 6) 0042 0043 class Record: 0044 0045 def __init__(self, PtrComp = None, Discr = 0, EnumComp = 0, 0046 IntComp = 0, StringComp = 0): 0047 self.PtrComp = PtrComp 0048 self.Discr = Discr 0049 self.EnumComp = EnumComp 0050 self.IntComp = IntComp 0051 self.StringComp = StringComp 0052 0053 def copy(self): 0054 return Record(self.PtrComp, self.Discr, self.EnumComp, 0055 self.IntComp, self.StringComp) 0056 0057 TRUE = 1 0058 FALSE = 0 0059 0060 def main(loops=LOOPS): 0061 benchtime, stones = pystones(loops) 0062 print "Pystone(%s) time for %d passes = %g" % \ 0063 (__version__, loops, benchtime) 0064 print "This machine benchmarks at %g pystones/second" % stones 0065 0066 0067 def pystones(loops=LOOPS): 0068 return Proc0(loops) 0069 0070 IntGlob = 0 0071 BoolGlob = FALSE 0072 Char1Glob = '\0' 0073 Char2Glob = '\0' 0074 Array1Glob = [0]*51 0075 Array2Glob = map(lambda x: x[:], [Array1Glob]*51) 0076 PtrGlb = None 0077 PtrGlbNext = None 0078 0079 def Proc0(loops=LOOPS): 0080 global IntGlob 0081 global BoolGlob 0082 global Char1Glob 0083 global Char2Glob 0084 global Array1Glob 0085 global Array2Glob 0086 global PtrGlb 0087 global PtrGlbNext 0088 0089 starttime = clock() 0090 for i in range(loops): 0091 pass 0092 nulltime = clock() - starttime 0093 0094 PtrGlbNext = Record() 0095 PtrGlb = Record() 0096 PtrGlb.PtrComp = PtrGlbNext 0097 PtrGlb.Discr = Ident1 0098 PtrGlb.EnumComp = Ident3 0099 PtrGlb.IntComp = 40 0100 PtrGlb.StringComp = "DHRYSTONE PROGRAM, SOME STRING" 0101 String1Loc = "DHRYSTONE PROGRAM, 1'ST STRING" 0102 Array2Glob[8][7] = 10 0103 0104 starttime = clock() 0105 0106 for i in range(loops): 0107 Proc5() 0108 Proc4() 0109 IntLoc1 = 2 0110 IntLoc2 = 3 0111 String2Loc = "DHRYSTONE PROGRAM, 2'ND STRING" 0112 EnumLoc = Ident2 0113 BoolGlob = not Func2(String1Loc, String2Loc) 0114 while IntLoc1 < IntLoc2: 0115 IntLoc3 = 5 * IntLoc1 - IntLoc2 0116 IntLoc3 = Proc7(IntLoc1, IntLoc2) 0117 IntLoc1 = IntLoc1 + 1 0118 Proc8(Array1Glob, Array2Glob, IntLoc1, IntLoc3) 0119 PtrGlb = Proc1(PtrGlb) 0120 CharIndex = 'A' 0121 while CharIndex <= Char2Glob: 0122 if EnumLoc == Func1(CharIndex, 'C'): 0123 EnumLoc = Proc6(Ident1) 0124 CharIndex = chr(ord(CharIndex)+1) 0125 IntLoc3 = IntLoc2 * IntLoc1 0126 IntLoc2 = IntLoc3 / IntLoc1 0127 IntLoc2 = 7 * (IntLoc3 - IntLoc2) - IntLoc1 0128 IntLoc1 = Proc2(IntLoc1) 0129 0130 benchtime = clock() - starttime - nulltime 0131 return benchtime, (loops / benchtime) 0132 0133 def Proc1(PtrParIn): 0134 PtrParIn.PtrComp = NextRecord = PtrGlb.copy() 0135 PtrParIn.IntComp = 5 0136 NextRecord.IntComp = PtrParIn.IntComp 0137 NextRecord.PtrComp = PtrParIn.PtrComp 0138 NextRecord.PtrComp = Proc3(NextRecord.PtrComp) 0139 if NextRecord.Discr == Ident1: 0140 NextRecord.IntComp = 6 0141 NextRecord.EnumComp = Proc6(PtrParIn.EnumComp) 0142 NextRecord.PtrComp = PtrGlb.PtrComp 0143 NextRecord.IntComp = Proc7(NextRecord.IntComp, 10) 0144 else: 0145 PtrParIn = NextRecord.copy() 0146 NextRecord.PtrComp = None 0147 return PtrParIn 0148 0149 def Proc2(IntParIO): 0150 IntLoc = IntParIO + 10 0151 while 1: 0152 if Char1Glob == 'A': 0153 IntLoc = IntLoc - 1 0154 IntParIO = IntLoc - IntGlob 0155 EnumLoc = Ident1 0156 if EnumLoc == Ident1: 0157 break 0158 return IntParIO 0159 0160 def Proc3(PtrParOut): 0161 global IntGlob 0162 0163 if PtrGlb is not None: 0164 PtrParOut = PtrGlb.PtrComp 0165 else: 0166 IntGlob = 100 0167 PtrGlb.IntComp = Proc7(10, IntGlob) 0168 return PtrParOut 0169 0170 def Proc4(): 0171 global Char2Glob 0172 0173 BoolLoc = Char1Glob == 'A' 0174 BoolLoc = BoolLoc or BoolGlob 0175 Char2Glob = 'B' 0176 0177 def Proc5(): 0178 global Char1Glob 0179 global BoolGlob 0180 0181 Char1Glob = 'A' 0182 BoolGlob = FALSE 0183 0184 def Proc6(EnumParIn): 0185 EnumParOut = EnumParIn 0186 if not Func3(EnumParIn): 0187 EnumParOut = Ident4 0188 if EnumParIn == Ident1: 0189 EnumParOut = Ident1 0190 elif EnumParIn == Ident2: 0191 if IntGlob > 100: 0192 EnumParOut = Ident1 0193 else: 0194 EnumParOut = Ident4 0195 elif EnumParIn == Ident3: 0196 EnumParOut = Ident2 0197 elif EnumParIn == Ident4: 0198 pass 0199 elif EnumParIn == Ident5: 0200 EnumParOut = Ident3 0201 return EnumParOut 0202 0203 def Proc7(IntParI1, IntParI2): 0204 IntLoc = IntParI1 + 2 0205 IntParOut = IntParI2 + IntLoc 0206 return IntParOut 0207 0208 def Proc8(Array1Par, Array2Par, IntParI1, IntParI2): 0209 global IntGlob 0210 0211 IntLoc = IntParI1 + 5 0212 Array1Par[IntLoc] = IntParI2 0213 Array1Par[IntLoc+1] = Array1Par[IntLoc] 0214 Array1Par[IntLoc+30] = IntLoc 0215 for IntIndex in range(IntLoc, IntLoc+2): 0216 Array2Par[IntLoc][IntIndex] = IntLoc 0217 Array2Par[IntLoc][IntLoc-1] = Array2Par[IntLoc][IntLoc-1] + 1 0218 Array2Par[IntLoc+20][IntLoc] = Array1Par[IntLoc] 0219 IntGlob = 5 0220 0221 def Func1(CharPar1, CharPar2): 0222 CharLoc1 = CharPar1 0223 CharLoc2 = CharLoc1 0224 if CharLoc2 != CharPar2: 0225 return Ident1 0226 else: 0227 return Ident2 0228 0229 def Func2(StrParI1, StrParI2): 0230 IntLoc = 1 0231 while IntLoc <= 1: 0232 if Func1(StrParI1[IntLoc], StrParI2[IntLoc+1]) == Ident1: 0233 CharLoc = 'A' 0234 IntLoc = IntLoc + 1 0235 if CharLoc >= 'W' and CharLoc <= 'Z': 0236 IntLoc = 7 0237 if CharLoc == 'X': 0238 return TRUE 0239 else: 0240 if StrParI1 > StrParI2: 0241 IntLoc = IntLoc + 7 0242 return TRUE 0243 else: 0244 return FALSE 0245 0246 def Func3(EnumParIn): 0247 EnumLoc = EnumParIn 0248 if EnumLoc == Ident3: return TRUE 0249 return FALSE 0250 0251 if __name__ == '__main__': 0252 import sys 0253 def error(msg): 0254 print >>sys.stderr, msg, 0255 print >>sys.stderr, "usage: %s [number_of_loops]" % sys.argv[0] 0256 sys.exit(100) 0257 nargs = len(sys.argv) - 1 0258 if nargs > 1: 0259 error("%d arguments are too many;" % nargs) 0260 elif nargs == 1: 0261 try: loops = int(sys.argv[1]) 0262 except ValueError: 0263 error("Invalid argument %r;" % sys.argv[1]) 0264 else: 0265 loops = LOOPS 0266 main(loops) 0267
Generated by PyXR 0.9.4