我不明白我是怎么工作的。我举了一个例子,我想计算处理时间的差异。如果有人有时间帮我把它拆了,我会永远感激的。你知道吗
低音
def main():
prod_nums = ['V475', 'F987', 'Q143', 'R688']
def search_fast(prod_nums):
for item in prod_nums:
if item == 'R688':
return True
return False
def search_slow(prod_nums):
return_value = False
for item in prod_nums:
if item == 'R688':
return_value = True
return return_value
如果要向函数传递参数,可能需要使用
timeit.Timer
,但要使列表全局化,如下所示:然后运行以下命令:
timeit
在您希望检查dev环境中的一小段代码时非常有用。你知道吗如果函数是这样的:
您可以使用
timeit.timeit
这不会回报你任何结果,只会回报你所花的时间。这是另一个可以使用decorator的场景。 基本上,您可以使用
timeit
来告诉您执行一个函数需要多少时间,就像终端中的time sample_file.py
。你知道吗基于python文档(https://docs.python.org/2/library/timeit.html):
This module provides a simple way to time small bits of Python code. It has both a Command-Line Interface as well as a callable one. It avoids a number of common traps for measuring execution times.
相关问题 更多 >
编程相关推荐