How to use python timeit when passing variables to functions?

后端 未结 5 772
迷失自我
迷失自我 2020-12-07 09:37

I\'m struggling with this using timeit and was wondering if anyone had any tips

Basically I have a function(that I pass a value to) that I want to test the speed of

5条回答
  •  一生所求
    2020-12-07 10:07

    One way to do it would be by using partial so that the function, 'superMegaIntenseFunction' is used as a callable (ie without the ()) in the timer or directly inside timeit.timeit. Using partial will pass the argument to the function when it will be call by the timer.

    from functools import partial
    from timeit import timeit
    
    print(timeit(partial(superMegaIntenseFunction, 10), number=1))
    

提交回复
热议问题