Python functools的部分效率 [英] Python functools partial efficiency
问题描述
import timeit
$ b我一直在使用Python,并设置了以下代码情形: $ b setting =
导入functools
def f(a,b,c):
通过
g = functools.partial(f ,c = 3)
h = functools.partial(f,b = 5,c = 3)
i = functools.partial(f,a = 4,b = 5,c = 3)
'
打印timeit.timeit('f(4,5,3)',setup = setting,number = 100000)
打印timeit.timeit('g(4, 5'',setup = setting,number = 100000)
print timeit.timeit('h(4)',setup = setting,number = 100000)
print timeit.timeit('i()' ,setup = setting,number = 100000)
我得到以下结果:
f:0.181384086609
g:0.39066195488
h:0.425783157349
i:0.391901016235
为什么对部分函数的调用需要更长的时间?部分函数只是将参数转发给原始函数,还是它将静态参数映射到整个函数?还有,Python中是否有函数返回填充函数的主体,因为所有参数都是预定义的,就像函数i一样? 解决方案
$ b
为什么对部分函数的调用需要更长的时间?由于附加的函数调用,
Python中的函数调用开销相对较高,尤其是与内置函数的执行速度相比较。
>
部分函数只是将参数转发给原始函数,还是将静态参数映射到整个函数?
blockquote>
据我所知 - 是的,它只是将参数转发给原始功能。
-
Python中的函数返回填充的函数的主体,因为所有参数都是预定义的,比如函数i?
否,我不知道Python中的这种内置函数。但我认为可以做你想做的事情,因为函数是可以被复制和修改的对象。
这是一个原型:
导入时间
导入类型
#http://stackoverflow.com/questions/6527633/how -can-i-make-a-python的深层拷贝
def copy_func(f,name = None):
返回types.FunctionType(f.func_code,f.func_globals ,名称或f.func_name,
f.func_defaults,f.func_closure)
def f(a,b,c):
返回a + b + c
i = copy_func(f,'i')
i.func_defaults =(4,5,3)
print timeit.timeit('f(4,5,3)',setup ='from __main__ import f',number = 100000)
print timeit.timeit('i()',setup ='from __main__ import i ',number = 100000)
给出:
0.0257439613342
0.0221881866455
I have been working with Python and I set up the following code situation:
import timeit setting = """ import functools def f(a,b,c): pass g = functools.partial(f,c=3) h = functools.partial(f,b=5,c=3) i = functools.partial(f,a=4,b=5,c=3) """ print timeit.timeit('f(4,5,3)', setup = setting, number=100000) print timeit.timeit('g(4,5)', setup = setting, number=100000) print timeit.timeit('h(4)', setup = setting, number=100000) print timeit.timeit('i()', setup = setting, number=100000)
I get the following as a result:
f: 0.181384086609 g: 0.39066195488 h: 0.425783157349 i: 0.391901016235
Why do the calls to the partial functions take longer? Is the partial function just forwarding the parameters to the original function or is it mapping the static arguments throughout? And also, is there a function in Python to return the body of a function filled in given that all the parameters are predefined, like with function i?
解决方案Why do the calls to the partial functions take longer?
The code with
partial
takes about two times longer because of the additional function call. Function calls are expensive:Function call overhead in Python is relatively high, especially compared with the execution speed of a builtin function.
-
Is the partial function just forwarding the parameters to the original function or is it mapping the static arguments throughout?
As far as i know - yes, it just forwards the arguments to the original function.
-
And also, is there a function in Python to return the body of a function filled in given that all the parameters are predefined, like with function i?
No, i am not aware of such built-in function in Python. But i think it's possible to do what you want, as functions are objects which can be copied and modified.
Here is a prototype:
import timeit import types # http://stackoverflow.com/questions/6527633/how-can-i-make-a-deepcopy-of-a-function-in-python def copy_func(f, name=None): return types.FunctionType(f.func_code, f.func_globals, name or f.func_name, f.func_defaults, f.func_closure) def f(a, b, c): return a + b + c i = copy_func(f, 'i') i.func_defaults = (4, 5, 3) print timeit.timeit('f(4,5,3)', setup = 'from __main__ import f', number=100000) print timeit.timeit('i()', setup = 'from __main__ import i', number=100000)
which gives:
0.0257439613342 0.0221881866455
这篇关于Python functools的部分效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!