内联函数调用 [英] inline function call

查看:120
本文介绍了内联函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我从一段时间以来都在搞怪,但找不到答案 - 也许是因为

答案是' '不!''。


我可以在python内联中调用一个函数,这样python字节编译器

实际上调用了函数,但是将它插入内联

调用的位置?因此避免了所有开销的功能。


谢谢和欢呼,


Riko

hi everyone,

I''m googeling since some time, but can''t find an answer - maybe because
the answer is ''No!''.

Can I call a function in python inline, so that the python byte compiler
does actually call the function, but sort of inserts it where the inline
call is made? Therefore avoiding the function all overhead.

Thanks and cheers,

Riko

推荐答案

我认为它确实存在。

但是应该是。


您也可以使用一些模板来卷起自己的库..

I think it does''n exists.
But should be.

You can also roll up your own using some templating library..


Riko Wichmann写道:
Riko Wichmann wrote:
我可以在python内联中调用一个函数,以便python字节编译器
实际上是调用函数,但是将它插入到内联
调用的位置?因此避免了所有开销的功能。
Can I call a function in python inline, so that the python byte compiler
does actually call the function, but sort of inserts it where the inline
call is made? Therefore avoiding the function all overhead.




不可以。在python和java中,函数

总是虚拟的,这根本不可能,意思是他们在运行时查找。因为你不会知道_which_代码插入所有不同的foo() -

的方法可能就在那里。


你有一个实际的用例吗?我的意思是,你的代码运行速度是否很慢,但内联代码的速度更快?


问候,


Diez



No. That is simply impossible in python as well as in java where functions
are always virtual, meaning they are looked up at runtime. Because you''d
never know _which_ code to insert of all the different foo()-methods that
might be around there.

Do you have an actual use-case for that? I mean, do you have code that runs
slow, but with inlined code embarrassingly faster?

Regards,

Diez


>你有实际的用例吗?我的意思是,你有没有运行
> Do you have an actual use-case for that? I mean, do you have code that runs
慢的代码,但内联代码比较快?
slow, but with inlined code embarrassingly faster?




好​​吧,我想它实际上不会令人尴尬的更快。从

尝试各种各样的事情并实际将功能代码复制到

DoMC例程中,我估计执行会减少15-20%

时间。它开始时非常慢,但是在应用了一些其他的'b $ b''fastpython'技术后,它实际上非常快......


''内联''现在主要是好奇心问题:)


这里是代码片段:


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


[...在这里删掉一些东西....]

#riskfunc(med,low,高):

#风险函数的成本:三角分布

#实现的依据:
http://www.brighton-webs.co.uk/distr...triangular.asp

def riskfunc(med,low,high):

if med!= 0.0:

u = random()

试试:

如果你< =(med-low)/(high-low):

r =低+ sqrt(u *(高 - 低)*(med-低))

其他:

r =高 - sqrt((1.0-u)*(高 - 低)*(高我) d))除了ZeroDivisionError之外
:#case high = low

r = med

else:

r = 0.0


返回r

#doMC:

#运行成本分析的MC



def doMC(Ntrial = 1):


来自数学输入sqrt


start = time .time()

打印''运行MC'',Ntrial,''试验''

#从定义的种子开始,用于再现性


总计= 0.0


我在范围内(Ntrial):


summe = 0.0

表示范围内的k(len(Gcost)):


x = riskfunc(Gcost [k],Gdown [k],Gup [k])

summe + = x


#存储值''summe''供以后使用

#.....更多代码在这里

print" Summe:",summe

stop = time.time()

print''计算时间:'',停止-start


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

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


if __name__ ==''__ main__'':


n = 100000

doMC(n)



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/distr...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)


这篇关于内联函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆