python - 将变量传递给函数时如何使用python timeit?

标签 python debugging benchmarking

我正在使用 timeit 来解决这个问题,想知道是否有人有任何提示

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

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

但是当我运行它时,我会收到一些奇怪的错误,比如来自 timeit 模块。:

ValueError: stmt is neither a string nor callable

如果我自己运行该函数,它可以正常工作。当我将它包装在它的模块中时,我得到了错误(我尝试使用双引号并且没有..sameoutput)。

任何建议都会很棒!

谢谢!

最佳答案

使其成为可调用对象:

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

应该有效

关于python - 将变量传递给函数时如何使用python timeit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7523767/

相关文章:

javascript - 当javascript进入无限循环和递归调用时如何调试javascript?

java - 如何用 Java 编写正确的微基准测试?

java - Android 和 Windows 基准测试(32 位)

c++ - Cython/C++ - 共享 PXD 文件中的头文件路径

python - 在两列中格式化 tkinter 消息框中的文本

javascript - Chrome 的 Firebug

Hadoop 基准测试/性能测试

python - 使用 boto HTTPS 上传到 S3?

python - 如何对文本文件执行多种功能

c# - Visual Studio : Make debugger aware that a function doesn't cause "side effects"