how to pass parameters of a function when using timeit.Timer()

前端 未结 12 1810
礼貌的吻别
礼貌的吻别 2020-12-02 10:19

This is the outline of a simple program

# some pre-defined constants
A = 1
B = 2

# function that does something critical
def foo(num1, num2):
    # do somet         


        
12条回答
  •  死守一世寂寞
    2020-12-02 11:01

    There is a much simpler solution (at least for Python 3), you can cause the code to be executed within your current global namespace:

    t = timeit.Timer(stmt="foo(num1,num2)", globals=globals())

    https://docs.python.org/3/library/timeit.html#examples I know globals are not preferred, but if you are just making a quick script to check something I think this is the easiest implementation.

提交回复
热议问题