how fast is Python?

Neil Padgen neil.padgen at mon.bbc.co.uk
Thu Aug 21 10:22:01 EDT 2003


On Wednesday 20 August 2003 21:19, Irmen de Jong wrote:
> Investigate Psyco.

On the strength of this thread, I investigated Psyco.  Results of a
very quick investigation with the following program:

-----------------------------------------
def calcPi(iterations):
    pi4 = 1.0
    for i in xrange(1, iterations):
        denominator = (4*i)-1
        pi4 = pi4 - 1.0/denominator + 1.0/(denominator+2)
    return pi4 * 4.0

def timethis(func, funcName):
    import sys
    try:
        i = int(sys.argv[1])
    except:
        i = 1000000
    import time
    start = time.time()
    pi = func(i)
    end = time.time()
    print "%s calculated pi as %s in %s seconds" % (funcName, pi, end
- start)

def main():
    timethis(calcPi, 'calcPi')
    timethis(speedyPi, 'speedyPi')

import psyco
speedyPi = psyco.proxy(calcPi)

if __name__ == '__main__':
    main()
-----------------------------------------

produced the following results on a 1.7GHz P4 running FreeBSD 4.8:

> python2.2 pi.py
calcPi calculated pi as 3.14159315359 in 3.87623202801 seconds
speedyPi calculated pi as 3.14159315359 in 0.790405035019 seconds

-- Neil




More information about the Python-list mailing list