| #! /usr/bin/python2.5 | | """ | "PYSTONE" Benchmark Program | | Version: Python/1.1 (corresponds to C/1.1 plus 2 Pystone fixes) | | Author: Reinhold P. Weicker, CACM Vol 27, No 10, 10/84 pg. 1013. | | Translated from ADA to C by Rick Richardson. | Every method to preserve ADA-likeness has been used, | at the expense of C-ness. | | Translated from C to Python by Guido van Rossum. | | Version History: | | Version 1.1 corrects two bugs in version 1.0: | | First, it leaked memory: in Proc1(), NextRecord ends | up having a pointer to itself. I have corrected this | by zapping NextRecord.PtrComp at the end of Proc1(). | | Second, Proc3() used the operator != to compare a | record to None. This is rather inefficient and not | true to the intention of the original benchmark (where | a pointer comparison to None is intended; the != | operator attempts to find a method __cmp__ to do value | comparison of the record). Version 1.1 runs 5-10 | percent faster than version 1.0, so benchmark figures | of different versions can't be compared directly. | | """ | | LOOPS = 50000 | | from time import clock | | __version__ = "1.1" | | [Ident1, Ident2, Ident3, Ident4, Ident5] = range(1, 6) | | class Record: | 0.173sec | def __init__(self, PtrComp = None, Discr = 0, EnumComp = 0, | IntComp = 0, StringComp = 0): 0.073sec | self.PtrComp = PtrComp 0.073sec | self.Discr = Discr 0.072sec | self.EnumComp = EnumComp 0.070sec | self.IntComp = IntComp 0.071sec | self.StringComp = StringComp | 0.165sec | def copy(self): 0.065sec | return Record(self.PtrComp, self.Discr, self.EnumComp, 0.080sec | self.IntComp, self.StringComp) | | TRUE = 1 | FALSE = 0 | | def main(loops=LOOPS): | benchtime, stones = pystones(loops) | print "Pystone(%s) time for %d passes = %g" % \ | (__version__, loops, benchtime) | print "This machine benchmarks at %g pystones/second" % stones | | 0.000sec | def pystones(loops=LOOPS): 0.000sec | return Proc0(loops) | | IntGlob = 0 | BoolGlob = FALSE | Char1Glob = '\0' | Char2Glob = '\0' | Array1Glob = [0]*51 | Array2Glob = map(lambda x: x[:], [Array1Glob]*51) | PtrGlb = None | PtrGlbNext = None | 0.000sec | def Proc0(loops=LOOPS): | global IntGlob | global BoolGlob | global Char1Glob | global Char2Glob | global Array1Glob | global Array2Glob | global PtrGlb | global PtrGlbNext | 0.000sec | starttime = clock() 0.065sec | for i in range(loops): 0.070sec | pass 0.001sec | nulltime = clock() - starttime | 0.000sec | PtrGlbNext = Record() 0.000sec | PtrGlb = Record() 0.000sec | PtrGlb.PtrComp = PtrGlbNext 0.000sec | PtrGlb.Discr = Ident1 0.000sec | PtrGlb.EnumComp = Ident3 0.000sec | PtrGlb.IntComp = 40 0.000sec | PtrGlb.StringComp = "DHRYSTONE PROGRAM, SOME STRING" 0.000sec | String1Loc = "DHRYSTONE PROGRAM, 1'ST STRING" 0.000sec | Array2Glob[8][7] = 10 | 0.000sec | starttime = clock() | 0.075sec | for i in range(loops): 0.075sec | Proc5() 0.073sec | Proc4() 0.073sec | IntLoc1 = 2 0.070sec | IntLoc2 = 3 0.070sec | String2Loc = "DHRYSTONE PROGRAM, 2'ND STRING" 0.070sec | EnumLoc = Ident2 0.070sec | BoolGlob = not Func2(String1Loc, String2Loc) 0.154sec | while IntLoc1 < IntLoc2: 0.075sec | IntLoc3 = 5 * IntLoc1 - IntLoc2 0.078sec | IntLoc3 = Proc7(IntLoc1, IntLoc2) 0.074sec | IntLoc1 = IntLoc1 + 1 0.083sec | Proc8(Array1Glob, Array2Glob, IntLoc1, IntLoc3) 0.077sec | PtrGlb = Proc1(PtrGlb) 0.079sec | CharIndex = 'A' 0.277sec | while CharIndex <= Char2Glob: 0.157sec | if EnumLoc == Func1(CharIndex, 'C'): | EnumLoc = Proc6(Ident1) 0.172sec | CharIndex = chr(ord(CharIndex)+1) 0.087sec | IntLoc3 = IntLoc2 * IntLoc1 0.077sec | IntLoc2 = IntLoc3 / IntLoc1 0.079sec | IntLoc2 = 7 * (IntLoc3 - IntLoc2) - IntLoc1 0.085sec | IntLoc1 = Proc2(IntLoc1) | 0.001sec | benchtime = clock() - starttime - nulltime 0.000sec | return benchtime, (loops / benchtime) | 0.144sec | def Proc1(PtrParIn): 0.065sec | PtrParIn.PtrComp = NextRecord = PtrGlb.copy() 0.078sec | PtrParIn.IntComp = 5 0.072sec | NextRecord.IntComp = PtrParIn.IntComp 0.075sec | NextRecord.PtrComp = PtrParIn.PtrComp 0.086sec | NextRecord.PtrComp = Proc3(NextRecord.PtrComp) 0.074sec | if NextRecord.Discr == Ident1: 0.075sec | NextRecord.IntComp = 6 0.072sec | NextRecord.EnumComp = Proc6(PtrParIn.EnumComp) 0.076sec | NextRecord.PtrComp = PtrGlb.PtrComp 0.077sec | NextRecord.IntComp = Proc7(NextRecord.IntComp, 10) | else: | PtrParIn = NextRecord.copy() 0.078sec | NextRecord.PtrComp = None 0.072sec | return PtrParIn | 0.160sec | def Proc2(IntParIO): 0.065sec | IntLoc = IntParIO + 10 0.069sec | while 1: 0.066sec | if Char1Glob == 'A': 0.071sec | IntLoc = IntLoc - 1 0.071sec | IntParIO = IntLoc - IntGlob 0.071sec | EnumLoc = Ident1 0.069sec | if EnumLoc == Ident1: 0.071sec | break 0.067sec | return IntParIO | 0.158sec | def Proc3(PtrParOut): | global IntGlob | 0.073sec | if PtrGlb is not None: 0.071sec | PtrParOut = PtrGlb.PtrComp | else: | IntGlob = 100 0.072sec | PtrGlb.IntComp = Proc7(10, IntGlob) 0.077sec | return PtrParOut | 0.146sec | def Proc4(): | global Char2Glob | 0.073sec | BoolLoc = Char1Glob == 'A' 0.072sec | BoolLoc = BoolLoc or BoolGlob 0.068sec | Char2Glob = 'B' | 0.148sec | def Proc5(): | global Char1Glob | global BoolGlob | 0.065sec | Char1Glob = 'A' 0.069sec | BoolGlob = FALSE | 0.145sec | def Proc6(EnumParIn): 0.065sec | EnumParOut = EnumParIn 0.066sec | if not Func3(EnumParIn): | EnumParOut = Ident4 0.074sec | if EnumParIn == Ident1: | EnumParOut = Ident1 0.074sec | elif EnumParIn == Ident2: | if IntGlob > 100: | EnumParOut = Ident1 | else: | EnumParOut = Ident4 0.074sec | elif EnumParIn == Ident3: 0.070sec | EnumParOut = Ident2 | elif EnumParIn == Ident4: | pass | elif EnumParIn == Ident5: | EnumParOut = Ident3 0.070sec | return EnumParOut | 0.434sec | def Proc7(IntParI1, IntParI2): 0.211sec | IntLoc = IntParI1 + 2 0.210sec | IntParOut = IntParI2 + IntLoc 0.208sec | return IntParOut | 0.155sec | def Proc8(Array1Par, Array2Par, IntParI1, IntParI2): | global IntGlob | 0.073sec | IntLoc = IntParI1 + 5 0.070sec | Array1Par[IntLoc] = IntParI2 0.072sec | Array1Par[IntLoc+1] = Array1Par[IntLoc] 0.078sec | Array1Par[IntLoc+30] = IntLoc 0.229sec | for IntIndex in range(IntLoc, IntLoc+2): 0.175sec | Array2Par[IntLoc][IntIndex] = IntLoc 0.078sec | Array2Par[IntLoc][IntLoc-1] = Array2Par[IntLoc][IntLoc-1] + 1 0.096sec | Array2Par[IntLoc+20][IntLoc] = Array1Par[IntLoc] 0.091sec | IntGlob = 5 | 0.450sec | def Func1(CharPar1, CharPar2): 0.215sec | CharLoc1 = CharPar1 0.199sec | CharLoc2 = CharLoc1 0.199sec | if CharLoc2 != CharPar2: 0.213sec | return Ident1 | else: | return Ident2 | 0.144sec | def Func2(StrParI1, StrParI2): 0.074sec | IntLoc = 1 0.138sec | while IntLoc <= 1: 0.071sec | if Func1(StrParI1[IntLoc], StrParI2[IntLoc+1]) == Ident1: 0.074sec | CharLoc = 'A' 0.075sec | IntLoc = IntLoc + 1 0.075sec | if CharLoc >= 'W' and CharLoc <= 'Z': | IntLoc = 7 0.075sec | if CharLoc == 'X': | return TRUE | else: 0.074sec | if StrParI1 > StrParI2: | IntLoc = IntLoc + 7 | return TRUE | else: 0.076sec | return FALSE | 0.157sec | def Func3(EnumParIn): 0.065sec | EnumLoc = EnumParIn 0.066sec | if EnumLoc == Ident3: return TRUE | return FALSE | | if __name__ == '__main__': | import sys | def error(msg): | print >>sys.stderr, msg, | print >>sys.stderr, "usage: %s [number_of_loops]" % sys.argv[0] | sys.exit(100) | nargs = len(sys.argv) - 1 | if nargs > 1: | error("%d arguments are too many;" % nargs) | elif nargs == 1: | try: loops = int(sys.argv[1]) | except ValueError: | error("Invalid argument %r;" % sys.argv[1]) | else: | loops = LOOPS | main(loops)