<html>
<body>
At 01:31 PM 1/12/2007, Dick Moores wrote:<br>
<blockquote type=cite class=cite cite="">At 06:16 AM 1/12/2007, Adam Bark
wrote:<br>
<blockquote type=cite class=cite cite="">On 11/01/07, <b>Danny Yoo</b>
&lt;<a href="mailto:dyoo@hkn.eecs.berkeley.edu">
dyoo@hkn.eecs.berkeley.edu</a>&gt; wrote:<br><br>
<dl>
<dd>&gt; Sometimes psyco speeds up a script by a factor of 10, and
sometimes 
<dd>&gt; it makes no difference at all. Here's a case where I fully
expected 
<dd>&gt; it to make a difference: 
<dd>&gt; &lt;
<a href="http://www.rcblue.com/Python/Psyco_Puzzle.txt" eudora="autourl">
http://www.rcblue.com/Python/Psyco_Puzzle.txt</a>&gt;. Whether using
psyco 
<dd>&gt; or not, this takes about 13 seconds on my computer. Why no
difference? 
<dd>Psyco implements a Just-In-Time optimization approach, so I suspect
that 
<dd>it does its analysis of a function only after that function has been
run 
<dd>at least once --- otherwise, it has no run-time information on which
it 
<dd>can use to analyze. 
<dd>In the code above, the code we want to optimize is fact().&nbsp;
However, 
<dd>fact() is only called one time in the whole program.&nbsp; To test
this 
<dd>hypothesis, it might be interesting to see if &quot;priming&quot;
fact up will help. 
<dd>############################################################# 
<dd>if __name__ == '__main__': 
<dd>&nbsp;&nbsp;&nbsp;&nbsp;
printFact(5)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
## just to prime the function up 
<dd>&nbsp;&nbsp;&nbsp;&nbsp; timeStart = time.time() 
<dd>&nbsp;&nbsp;&nbsp;&nbsp; printFact(20000) 
<dd>&nbsp;&nbsp;&nbsp;&nbsp; timeEnd = time.time() 
<dd>&nbsp;&nbsp;&nbsp;&nbsp; print &quot;Time was %.4g seconds&quot; %
(timeEnd - timeStart) 
<dd>#############################################################<br>

<dd>Furthermore, the magnitude of the numbers in the fact() code quickly
get 
<dd>into bignum range, where psyco's optimizations probably won't be so 
<dd>effective.&nbsp; In contrast, the primes code you have all deal with
integers 
<dd>in the range of 32 bits.<br><br>
</dl><br>
I tested this myself and it looks like bignum is probably the slowdown
here<br>
without psyco:<br>
20000! = 1.81e+77337<br>
Time was 7.58 seconds<br>
with psyco no priming:<br>
20000! = 1.81e+77337<br>
Time was 7.55 seconds<br>
with psyco and priming:<br>
5! = 1.20e+002<br>
20000! = 1.81e+77337<br>
Time was 7.591 seconds<br><br>
there seems to be no difference with psyco or without even if you run
the<br>
function first.</blockquote><br>
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.<br><br>
Danny suggested asking on the psyco list. I'll report back here what I
find out.<br><br>
But another question. I tried testing just my function fact() (see &lt;
http://www.rcblue.com/Python/Psyco_Puzzle.txt&gt;) using timeit.py's
template, with and without psyco. Without psyco I used<br>
=============================<br>
def inner(_it, _timer):<br>
&nbsp;&nbsp;&nbsp; from mine.mycalc import fact<br>
&nbsp;&nbsp;&nbsp; _t0 = _timer()<br>
&nbsp;&nbsp;&nbsp; for _i in _it:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fact (5)<br>
&nbsp;&nbsp;&nbsp; _t1 = _timer()<br>
&nbsp;&nbsp;&nbsp; return _t1 - _t0<br>
=============================<br>
and got 100000 loops, best of 3: 2.35 usec per loop<br><br>
With psyco I used<br>
=================================<br>
def inner(_it, _timer):<br>
&nbsp;&nbsp;&nbsp; from mine.mycalc import fact; import psyco<br>
&nbsp;&nbsp;&nbsp; _t0 = _timer()<br>
&nbsp;&nbsp;&nbsp; for _i in _it:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; psyco.full();fact (5)<br>
&nbsp;&nbsp;&nbsp; _t1 = _timer()<br>
&nbsp;&nbsp;&nbsp; return _t1 - _t0<br>
=================================<br>
and got 100000 loops, best of 3: 15.5 usec per loop! A slowdown WITH
psyco of&nbsp; 6.6 times.<br><br>
With fact(500) in the template:<br>
without psyco, 1000 loops, best of 3: 822 usec per loop<br>
with psyco, 1000 loops, best of 3: 632 usec per loop, a slight
speed-up<br><br>
<br>
With fact(5000) in the template:<br>
without psyco: 10 loops, best of 3: 61.5 msec per loop<br>
with psyco: 10 loops, best of 3: 59.9 msec per loop, essentially no
speed-up.<br><br>
With fact(20000) in the template:<br>
without psyco: 10 loops, best of 3: 1.05 sec per loop<br>
with psyco: 10 loops, best of 3: 1.05 sec per loop, zero
speed-up</blockquote><br>
After changing the template to use psyco in what I'm now guessing is the
correct way:<br><br>
without psyco (same template as before):<br>
=========================<br>
def inner(_it, _timer):<br>
&nbsp;&nbsp;&nbsp; from mine.mycalc import fact<br>
&nbsp;&nbsp;&nbsp; _t0 = _timer()<br>
&nbsp;&nbsp;&nbsp; for _i in _it:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fact (5)<br>
&nbsp;&nbsp;&nbsp; _t1 = _timer()<br>
&nbsp;&nbsp;&nbsp; return _t1 - _t0<br>
===========================<br>
result: 100000 loops, best of 3: 1.87 usec per loop<br><br>
with psyco:<br>
==========================<br>
def inner(_it, _timer):<br>
&nbsp;&nbsp;&nbsp; from mine.mycalc import fact; import
psyco;psyco.full()<br>
&nbsp;&nbsp;&nbsp; _t0 = _timer()<br>
&nbsp;&nbsp;&nbsp; for _i in _it:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fact (5)<br>
&nbsp;&nbsp;&nbsp; _t1 = _timer()<br>
&nbsp;&nbsp;&nbsp; return _t1 - _t0<br>
======================<br>
result: 10000000 loops, best of 3: 0.0579 usec per loop (a speed-up of 32
times!)<br><br>
With fact(500) in the template:<br>
without psyco: 1000 loops, best of 3: 677 usec per loop<br>
with psyco: 1000 loops, best of 3: 507 usec per loop (a speed-up of 1.3
times)<br><br>
With fact(5000) in the template:<br>
without psyco: 10 loops, best of 3: 59.6 msec per loop<br>
with psyco: 10 loops, best of 3: 58.6 msec per loop (essentially no
speed-up)<br><br>
With fact(20000) in the template:<br>
without psyco: 10 loops, best of 3: 850 msec per loop<br>
with psyco: 10 loops, best of 3: 840 msec per loop (essentially no
speed-up)<br><br>
So that's further support for Danny's hypothesis.<br><br>
However, Danny's &quot;priming psyco by running the function once&quot;
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.<br><br>
=============================<br>
def inner(_it, _timer):<br>
&nbsp;&nbsp;&nbsp; from mine.mycalc import fact; import
psyco;psyco.full(); <b>fact(5)<br>
</b>&nbsp;&nbsp;&nbsp; _t0 = _timer()<br>
&nbsp;&nbsp;&nbsp; for _i in _it:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fact (20000)<br>
&nbsp;&nbsp;&nbsp; _t1 = _timer()<br>
&nbsp;&nbsp;&nbsp; return _t1 - _t0<br>
=============================<br><br>
Dick <br><br>
<br><br>
<br><br>
<br>
</body>
<br>
</html>