[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

A question about osyco

iu2

1/28/2008 1:32:00 PM

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
3 Answers

Marc 'BlackJack' Rintsch

1/28/2008 5:33:00 PM

0

On Mon, 28 Jan 2008 05:31:41 -0800, iu2 wrote:

> 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

Bearophile

1/28/2008 11:48:00 PM

0

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

iu2

1/29/2008 6:01:00 AM

0

On Jan 29, 1:48 am, bearophileH...@lycos.com wrote:
> 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

Thanks, that's probably it

I've tested

>>> timeit.Timer('c+d', 'from __main__ import c, d').timeit(1000)
6.6209532214145383e-005
>>> timeit.Timer('a+b', 'from __main__ import a, b').timeit(1000)
0.10513989906537802
>>>

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