A question about osyco

I

iu2

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
 
M

Marc 'BlackJack' Rintsch

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?

Try calling the iterative one twice and measure the time of the second
call. IIRC psyco needs at least one call to analyze the function, so the
first call is not speed up.

Ciao,
Marc 'BlackJack' Rintsch
 
B

bearophileHUGS

Marc 'BlackJack' Rintsch:
Try calling the iterative one twice and measure the time of the second
call. IIRC psyco needs at least one call to analyze the function, so the
first call is not speed up.

That's how Java HotSpot works, but Psyco is very different from
HotSpot, and I think you are wrong.
I don't exactly know the answer to the question of the OP, but I think
the two functions are different: in the second function most time
isn't spent in managing the xrange or in the assign, but in the sum
between long ints, and Psyco can't speed up that operation (you need
gmpy for that, and in certain tricky situations gmpy may even result
slower, maybe when you use small numbers).

Bye,
bearophile
 
I

iu2

Marc 'BlackJack' Rintsch:


That's how Java HotSpot works, but Psyco is very different from
HotSpot, and I think you are wrong.
I don't exactly know the answer to the question of the OP, but I think
the two functions are different: in the second function most time
isn't spent in managing the xrange or in the assign, but in the sum
between long ints, and Psyco can't speed up that operation (you need
gmpy for that, and in certain tricky situations gmpy may even result
slower, maybe when you use small numbers).

Bye,
bearophile

Thanks, that's probably it

I've tested

where c and d are equal to 1, and a, b are very long integers
(a=b=fib2(100000))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top