传递变量到函数时如何使用python timeit? [英] How to use python timeit when passing variables to functions?

查看:192
本文介绍了传递变量到函数时如何使用python timeit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用这个时间,并且想知道是否有人有任何提示



基本上我有一个函数(我传递一个值),我想要测试速度并创建这个:

  if __name __ =='__ main__':
from timeit import Timer
t = Timer(superMegaIntenseFunction(10))
打印t.timeit(number = 1)

但是当我运行它时,我会发现奇怪的错误,如来自timeit模块。

  ValueError:stmt既不是一个字符串也不可调用

如果我自己运行该功能,它的工作正常。当我把它包装在它的模块的时间,我得到的错误(我已经尝试使用双引号,没有..sameoutput)。



任何建议将是真棒!



谢谢!

解决方案

使其成为可调用:

  if __name __ =='__ main__':
from timeit import Timer
t = Timer(lambda:superMegaIntenseFunction(10) )
print t.timeit(number = 1)

应该工作


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 and created this:

if __name__=='__main__':
    from timeit import Timer
    t = Timer(superMegaIntenseFunction(10))
    print t.timeit(number=1)

but when I run it, I get weird errors like coming from the timeit module.:

ValueError: stmt is neither a string nor callable

If I run the function on its own, it works fine. Its when I wrap it in the time it module, I get the errors(I have tried using double quotes and without..sameoutput).

any suggestions would be awesome!

Thanks!

解决方案

Make it a callable:

if __name__=='__main__':
    from timeit import Timer
    t = Timer(lambda: superMegaIntenseFunction(10))
    print t.timeit(number=1)

Should work

这篇关于传递变量到函数时如何使用python timeit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆