On 11/01/07, Danny Yoo < [EMAIL PROTECTED]> wrote:
- > Sometimes psyco speeds up a script by a factor of 10, and sometimes
- > it makes no difference at all. Here's a case where I fully expected
- > it to make a difference:
- > < http://www.rcblue.com/Python/Psyco_Puzzle.txt>. Whether using psyco
- > or not, this takes about 13 seconds on my computer. Why no difference?
- Psyco implements a Just-In-Time optimization approach, so I suspect that
- it does its analysis of a function only after that function has been run
- at least once --- otherwise, it has no run-time information on which it
- can use to analyze.
- In the code above, the code we want to optimize is fact(). However,
- fact() is only called one time in the whole program. To test this
- hypothesis, it might be interesting to see if "priming" fact up will help.
- #############################################################
- if __name__ == '__main__':
- printFact(5) ## just to prime the function up
- timeStart = time.time()
- printFact(20000)
- timeEnd = time.time()
- print "Time was %.4g seconds" % (timeEnd - timeStart)
- #############################################################
- Furthermore, the magnitude of the numbers in the fact() code quickly get
- into bignum range, where psyco's optimizations probably won't be so
- effective. In contrast, the primes code you have all deal with integers
- in the range of 32 bits.
I tested this myself and it looks like bignum is probably the slowdown here
without psyco:
20000! = 1.81e+77337
Time was 7.58 seconds
with psyco no priming:
20000! = 1.81e+77337
Time was 7.55 seconds
with psyco and priming:
5! = 1.20e+002
20000! = 1.81e+77337
Time was 7.591 seconds
there seems to be no difference with psyco or without even if you run the
function first.
Yes, Danny's hunch about bignum seems to be correct. With smaller n, I do see some speed-up with psyco, but no more than 2 times.
Danny suggested asking on the psyco list. I'll report back here what I find out.
But another question. I tried testing just my function fact() (see < http://www.rcblue.com/Python/Psyco_Puzzle.txt>) using timeit.py's template, with and without psyco. Without psyco I used
=============================
def inner(_it, _timer):
from mine.mycalc import fact
_t0 = _timer()
for _i in _it:
fact (5)
_t1 = _timer()
return _t1 - _t0
=============================
and got 100000 loops, best of 3: 2.35 usec per loop
With psyco I used
=================================
def inner(_it, _timer):
from mine.mycalc import fact; import psyco
_t0 = _timer()
for _i in _it:
psyco.full();fact (5)
_t1 = _timer()
return _t1 - _t0
=================================
and got 100000 loops, best of 3: 15.5 usec per loop! A slowdown WITH psyco of 6.6 times.
With fact(500) in the template:
without psyco, 1000 loops, best of 3: 822 usec per loop
with psyco, 1000 loops, best of 3: 632 usec per loop, a slight speed-up
With fact(5000) in the template:
without psyco: 10 loops, best of 3: 61.5 msec per loop
with psyco: 10 loops, best of 3: 59.9 msec per loop, essentially no speed-up.
With fact(20000) in the template:
without psyco: 10 loops, best of 3: 1.05 sec per loop
with psyco: 10 loops, best of 3: 1.05 sec per loop, zero speed-up
Why that anomaly at fact(5)? Am I using the template correctly? Or psyco shouldn't be used with timeit.py?
Dick
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor