inline function call

Riko Wichmann riko.wichmann-remove at this-too.web.de
Wed Jan 4 16:07:15 EST 2006


Hi Peter,

> Riko, any chance you could post the final code and a bit more detail on 
> exactly how much Psyco contributed to the speedup?  The former would be 
> educational for all of us, while I'm personally very curious about the 
> latter because my limited attempts to use Psyco in the past have 
> resulted in speedups on the order of only 20% or so.  (I blame my 
> particular application, not Psyco per se, but I'd be happy to see a 
> real-world case where Psyco gave a much bigger boost.)

the difference between running with and without psyco is about a factor 
3 for my MC simulation. Without psyco the simulation runs for 62 sec, 
with it for 19 secs (still using time instead of timeit, though!:) This 
is for about 2300 and 10000 in for the inner and outer loop, respectively.

A factor 3 I consider worthwhile, especially since it doesn't really 
cost you much.

This is on a Dell Lat D600 running Linux (Ubuntu 5.10) with a 1.6 GHz 
Pentium M and 512 MB of RAM and python2.4.

The final code snipplet is attached. However, it is essentially 
unchanged compared to the piece I posted earlier which already had most 
of the global namespace look-up removed. Taking care of sqrt and random 
as you suggested didn't improve much anymore. So it's probably not that 
educational afterall.

Cheers,

	Riko

-----------------------------------------------------


# import some modules
import string
import time
from math import sqrt

# accelerate:
import psyco

# random number init
from random import random, seed
seed(1)



# riskfunc(med, low, high):
#           risk function for costs: triangular distribution
#           implemented acoording to: 
http://www.brighton-webs.co.uk/distributions/triangular.asp
def riskfunc(med, low, high):


     if med != 0.0:
         u = random()
         try:
             if u <= (med-low)/(high-low):
                 r = low+sqrt(u*(high-low)*(med-low))
             else:
                 r = high - sqrt((1.0-u)*(high-low)*(high-med))

         except ZeroDivisionError: # case high = low
                 r = med
     else:
         r = 0.0

     return r


# doMC:
#      run the MC of the cost analysis
#
def doMC(Ntrial = 1):

     start = time.time()
     print 'run MC with ', Ntrial, ' trials'


     # now do MC simulation and calculate sums

     for i in range(Ntrial):

         summe = 0.0
         # do MC experiments for all cost entries
         for k in range(len(Gcost)):
             x = riskfunc(Gcost[k], Gdown[k], Gup[k])
             summe +=x

         if i%(Ntrial/10) == 0:
             print i, 'MC experiment processed, Summe = %10.2f' % (summe)

     stop = time.time()
     print 'Computing time: ', stop-start



####################################################################
####################################################################

if __name__ == '__main__':

     fname_base = 'XFEL_budget-book_Master-2006-01-02_cost'

     readCosts(fname_base+'.csv')

     psyco.full()

     n = 10000
     doMC(n)





More information about the Python-list mailing list