[Tutor] Psyco Puzzle

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Jan 11 23:13:47 CET 2007



> 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.

Unfortunately, I can't test psyco, so there might be another factor that 
I'm missing.  If you want an authoritative answer, I'd recommend asking 
your question on the psyco mailing list: they have more experience with 
what psyco's good at.


One other thing to note: something looks weird:

     if __name__ == '__main__'== '__main__':
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This looks off.  Can you correct it?


Good luck!


More information about the Tutor mailing list