Python functools partial efficiency

后端 未结 2 778
广开言路
广开言路 2020-12-09 04:03

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 = func         


        
2条回答
  •  粉色の甜心
    2020-12-09 04:24

    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
    

提交回复
热议问题