# All code Copyright 2004 Christopher W. Cowell-Shah import time import math import sys # The psyco package (psyco.sourceforge.net) does JIT compiling of Python code. Can produce huge speedups. Uncomment next two lines to enable psyco. #import psyco #psyco.full() intMax = 1000000000 # 1B doubleMin = 10000000000.0 # 10B doubleMax = 11000000000.0 # 11B longMin = 10000000000L # 10B longMax = 11000000000L # 11B trigMax = 10000000.0 # 10M ioMax = 1000000 # 1M def intArithmetic(intMax): startTime = time.clock() i = 1 intResult = 1 while i < intMax: intResult = intResult - i i = i + 1 intResult = intResult + i i = i + 1 intResult = intResult * i i = i + 1 intResult = intResult / i i = i + 1 stopTime = time.clock() elapsedTime = (stopTime - startTime) * 1000 # convert from secs to millisecs. print "Int arithmetic elapsed time:", elapsedTime, "ms with intMax of", intMax print " i:", i print " intResult:", intResult return elapsedTime def doubleArithmetic(doubleMin, doubleMax): startTime = time.clock() i = doubleMin doubleResult = doubleMin while i < doubleMax: doubleResult = doubleResult - i i = i + 1.0 doubleResult = doubleResult + i i = i + 1.0 doubleResult = doubleResult * i i = i + 1.0 doubleResult = doubleResult / i i = i + 1.0 stopTime = time.clock() elapsedTime = (stopTime - startTime) * 1000 # convert from secs to millisecs. print "Double arithmetic elapsed time:", elapsedTime, "ms with doubleMin of", doubleMin, ", doubleMax of", doubleMax print " i:", i print " doubleResult:", doubleResult return elapsedTime def longArithmetic(longMin, longMax): startTime = time.clock() i = longMin longResult = longMin while i < longMax: longResult = longResult - i i = i + 1L longResult = longResult + i i = i + 1L longResult = longResult * i i = i + 1L longResult = longResult / i i = i + 1L stopTime = time.clock() elapsedTime = (stopTime - startTime) * 1000 # convert from secs to millisecs. print "Long arithmetic elapsed time:", elapsedTime, "ms with longMin of", longMin, ", longMax of", longMax print " i:", i print " longResult:", longResult return elapsedTime def trig(trigMax): startTime = time.clock() i = 1.0 sine = 0.0 cosine = 0.0 tangent = 0.0 logarithm = 0.0 squareRoot = 0.0 while i < trigMax: sine = math.sin(i) cosine = math.cos(i) tangent = math.tan(i) logarithm = math.log10(i) squareRoot = math.sqrt(i) i = i + 1.0 stopTime = time.clock() elapsedTime = (stopTime - startTime) * 1000 # convert from secs to millisecs. print "Trig elapsed time:", elapsedTime, "ms with trigMax of", trigMax print " i:", i print " sine:", sine print " cosine:", cosine print " tangent:", tangent print " logarithm:", logarithm print " squareRoot:", squareRoot return elapsedTime def io(ioMax): startTime = time.clock() fileName = "C:\TestPython.txt" myString = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefgh\n" file = open(fileName, 'w') linesToWrite = [myString] for i in range(ioMax - 1): linesToWrite.append(myString) file.writelines(linesToWrite) file.close() file = open(fileName, 'r') readLines = file.readlines() file.close() stopTime = time.clock() elapsedTime = (stopTime - startTime) * 1000 # convert from secs to millisecs. print "IO elapsed time: ", elapsedTime, "with ioMax of", ioMax print " len(linesToWrite):", len(linesToWrite) print " len(readLines):", len(readLines) print " readLines.last:", readLines[len(readLines) - 1:len(readLines)] return elapsedTime # Main program begins here print "Start Python benchmark" intArithmeticTime = intArithmetic(intMax) doubleArithmeticTime = doubleArithmetic(doubleMin, doubleMax) longArithmeticTime = longArithmetic(longMin, longMax) trigTime = trig(trigMax) ioTime = io(ioMax) totalTime = intArithmeticTime + doubleArithmeticTime + longArithmeticTime + trigTime + ioTime print "Total Python benchmark time:", totalTime, "ms" print "End Python benchmark"