import os import psyco def time(fn, *args): "Measure the execution time of fn(*args)." import time, traceback begin = time.clock() try: result = fn(*args) except: end = time.clock() traceback.print_exc() result = '' else: end = time.clock() return result, end-begin ZERO = 0 def f1(n): "Arbitrary test function." i = 0 x = 1 while i 2: if i > 126-33: i = 126-33 return chr(33+i) return " " def f7(start, end, step): "Computes the Mandelbrot set. All args are complex numbers." width = int((end.real - start.real) / step.real) while start.imag < end.imag: line = [mandelbrot(start + n*step.real) for n in range(width)] print ''.join(line) start += complex(0, step.imag) def f8(): try: x = 1.0 / 0.0 #except StandardError, e: # print "in except clause:", e finally: print "in finally clause" x = 5 print "x is now", x def f9(n): for i in range(n): print i, print def f10(): apply(f9, (50,)) def f11(n): def subtracter(x, y=n): return x-y return subtracter(1000) def go(f, *args): print '-'*80 v1, t1 = time(psyco.proxy(f), *args) print v1 print '^^^ computed by Psyco in %s seconds' % t1 v2, t2 = time(f, *args) if v1 == v2: if t1 and t2: s = ', Psyco is %.2f times faster' % (t2/float(t1)) else: s = '' print 'Python got the same result in %s seconds%s' % (t2, s) else: print v2 print '^^^ by Python in %s seconds' % t2 print '*'*80 print '!!! different results !!!' def go1(arg=2117): go(f1, arg) FILEPATH = '../c' FILELIST = [os.path.join(FILEPATH, s) for s in os.listdir(FILEPATH)] FILELIST = [s for s in FILELIST if os.path.isfile(s) and s!='psyco.dump'] def go4(arg=FILELIST): go(f4, arg) def go5(arg=FILELIST): go(f5, arg) def go6(n=100000, p=100000000000001L): go(f6, n, p) def go7(start=-2-1j, end=1+1j, step=0.04+0.08j): go(f7, start, end, step) if __name__ == "__main__": go(f11, 50) go1() go4() go5() go6() go7() go(f10)