inline function call
Riko Wichmann
riko.wichmann at remove-this.desy.de
Wed Jan 4 10:40:56 EST 2006
> Do you have an actual use-case for that? I mean, do you have code that runs
> slow, but with inlined code embarrassingly faster?
Well, I guess it would not actually be embarrassingly faster. From
trying various things and actually copying the function code into the
DoMC routine, I estimate to get about 15-20% reduction in the execution
time. It ran very slow, in the beginning but after applying some other
'fastpython' techniques it's actually quite fast ....
'inlining' is mostly a matter of curiosity now :)
here is the code snipplet:
-----------------------------------------------------------------
[... cut out some stuff here ....]
# 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):
from math import sqrt
start = time.time()
print 'run MC with ', Ntrial, ' trials'
# start with a defined seed for reproducability
total = 0.0
for i in range(Ntrial):
summe = 0.0
for k in range(len(Gcost)):
x = riskfunc(Gcost[k], Gdown[k], Gup[k])
summe += x
# store the value 'summe' for later usage
# ..... more code here
print "Summe : ", summe
stop = time.time()
print 'Computing time: ', stop-start
####################################################################
####################################################################
if __name__ == '__main__':
n = 100000
doMC(n)
More information about the Python-list
mailing list