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

前端 未结 12 1785
礼貌的吻别
礼貌的吻别 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:12

    This should work:

    import timeit
    
    def f(x,y):
        return x*y
    
    x = 5
    y = 7
    
    print(timeit.timeit(stmt='f(x,y)',
                        setup='from __main__ import f, x, y',
                        number=1000))
    

提交回复
热议问题