A question about osyco
iu2
israelu at elbit.co.il
Mon Jan 28 08:31:41 EST 2008
Hi guys,
I wrote two version of a fib functions, a recursive one and an
iterative one.
Psyco improved a lot the recursive function time, but didn't affect at
all the iterative function.
Why?
Here is the code:
import time, psyco
def mytime(code):
t = time.time()
res = eval(code)
delta = time.time() - t
print 'Code:', code
print 'Result:', str(res) if len(str(res)) < 20 else str(res)[:10]
+ '...'
print 'Time:', delta
print
def fib1(n):
if n <= 1: return 1
return fib1(n-2) + fib1(n-1)
def fib2(n):
x, y = 1, 1
for i in xrange(n-1):
x, y = y, x + y
return y
mytime('fib1(32)')
print 'psyco'
psyco.bind(fib1)
mytime('fib1(32)')
mytime('fib2(100000)')
print 'psyco'
psyco.bind(fib2)
mytime('fib2(100000)')
And the output:
----------------------
Code: fib1(32)
Result: 3524578
Time: 2.74499988556
psyco
Code: fib1(32)
Result: 3524578
Time: 0.119999885559
Code: fib2(100000)
Result: 4202692702...
Time: 5.53900003433
psyco
Code: fib2(100000)
Result: 4202692702...
Time: 5.46900010109
More information about the Python-list
mailing list