At 06:16 AM 1/12/2007, Adam Bark wrote:
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
After changing the template to use psyco in what I'm now guessing is the correct way:
without psyco (same template as before):
=========================
def inner(_it, _timer):
from mine.mycalc import fact
_t0 = _timer()
for _i in _it:
fact (5)
_t1 = _timer()
return _t1 - _t0
===========================
result: 100000 loops, best of 3: 1.87 usec per loop
with psyco:
==========================
def inner(_it, _timer):
from mine.mycalc import fact; import psyco;psyco.full()
_t0 = _timer()
for _i in _it:
fact (5)
_t1 = _timer()
return _t1 - _t0
======================
result: 10000000 loops, best of 3: 0.0579 usec per loop (a speed-up of 32 times!)
With fact(500) in the template:
without psyco: 1000 loops, best of 3: 677 usec per loop
with psyco: 1000 loops, best of 3: 507 usec per loop (a speed-up of 1.3 times)
With fact(5000) in the template:
without psyco: 10 loops, best of 3: 59.6 msec per loop
with psyco: 10 loops, best of 3: 58.6 msec per loop (essentially no speed-up)
With fact(20000) in the template:
without psyco: 10 loops, best of 3: 850 msec per loop
with psyco: 10 loops, best of 3: 840 msec per loop (essentially no speed-up)
So that's further support for Danny's hypothesis.
However, Danny's "priming psyco by running the function once" hypothesis seems to be disconfirmed. No improvement in speeds. This is the template I used. Note the addition of fact(5) to the end of the 2nd line. This would run fact(5) once before the timing starts, I believe.
=============================
def inner(_it, _timer):
from mine.mycalc import fact; import psyco;psyco.full(); fact(5)
_t0 = _timer()
for _i in _it:
fact (20000)
_t1 = _timer()
return _t1 - _t0
=============================
Dick
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor