我必须在1000次运行中找到我的函数运行时间的平均值.我应该使用哪些代码使其运行1000次然后找到它们的平均值?我的功能是:
import time
t0 = time.clock()
def binary_search(my_list, x):
left=0
right=len(my_list)-1
while left<=right:
mid = (left+right)//2
if my_list[mid]==x:
return True
elif my_list[mid] < x: #go to right half
left = mid+1
else: #go to left half
right = mid-1
return False #if we got here the search failed
t1 = time.clock()
print("Running time: ", t1-t0, "sec")
Run Code Online (Sandbox Code Playgroud)