python - 如果我使用 timeit 计时函数需要多长时间,如何将函数的结果存储在变量中?

标签 python timeit

我必须计算返回列表的函数运行需要多长时间。我想将运行所需的时间存储在变量中,并存储函数返回的列表。我该如何存储两者?

返回时间但丢失结果的示例:

t1 = timeit.timeit(myFunction())

如果 myFunction 返回一个列表,我如何存储它和时间?这可能吗?

最佳答案

你可以用“老式方式”来做:

start = time.time()
var = myFunction()
end = time.time()
time_needed = end - start
print(var, time_needed)

关于python - 如果我使用 timeit 计时函数需要多长时间,如何将函数的结果存储在变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46967369/

相关文章:

python - 我使用 PyPy 错了吗?它比标准 Python 慢 10 倍

python - Django 查询给定位置的有序对象

python - 为什么 lambda 函数执行时间在作为可调用函数传递时与作为字符串语句传递给 timeit.repeat() 时不同?

python - 时间多个python脚本

python - IPython 中 %time 和 %timeit 之间的不一致

python - pip 安装后错误 "no module named six"

python - 通过对 pandas 中的列值进行分组来拆分 DataFrame

python - 初学者 : python len() closes file?

python - 无法在 python 中将数据对象与 timeit.Time 模块一起使用

python - 使用 timeit 时导入的正确方法?