简体   繁体   English

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

[英]How do I store the result of a function in a variable if I'm timing how long it takes using timeit?

I have to time how long it takes a function to run that returns a list.我必须计算运行返回列表的函数需要多长时间。 I want to store the time it takes it to run in a variable, and store the list the function returns.我想将它运行的时间存储在一个变量中,并存储函数返回的列表。 How do I go about storing both?我如何去存储两者?

Example that returns the time but loses the result:返回时间但丢失结果的示例:

t1 = timeit.timeit(myFunction())

If myFunction returns a list, how do I store it and the time?如果 myFunction 返回一个列表,我该如何存储它和时间? Is this possible?这可能吗?

You can do it the "old fashion way":你可以用“旧时尚的方式”做到这一点:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM