我无法使此代码正常工作?这段代码应该找出程序为 find[42] 花费了多长时间 这是一个 python 代码
I am having trouble getting this code to work? This code is suppoed to find out how long it took for the program for fing d[42] This is a python code
当我尝试 运行 这段代码时,我应该获取程序定位的时间值 [42] 但我一直收到错误
import random
import time
ranges = [1000, 10000, 100000, 1000000]
dict_times = []
index_times = []
sort_times = []
for i in ranges:
L = random.sample(range(i), i)
d = dict(zip(L,L))
#time for dict ------
#start the time here
start = time.time()
v = d[42]
#end the time here
end = time.time()
dict_times.append(end-start)
#time for index ------
#start the time here
start = time.time()
ind = L.index(42)
#end the time here
end = time.time()
index_times.append(end-start)
#time for sort -----
#start the time here
start = time.time()
L = sorted(L)
#end the time here
end = time.time()
sort_times.append(end-start)
print('Times for dictionary lookup:', dict_times, end="\n\n")
print('Times for list lookup:', index_times)
index_ratios = [round(index_times[i]/index_times[i-1], 2) for i in range(1, len(index_times))]
print('List lookup Ratios:', index_ratios, end="\n\n")
print('Times for list sort:', sort_times)
sort_ratios = [round(sort_times[i]/sort_times[i-1], 2) for i in range(1, len(sort_times))]
print('List sort Ratios:', sort_ratios, end="\n\n")
list_sort_over_index = [round(sort_times[i]/index_times[i], 2) for i in range(len(sort_times))]
print('List sort over list lookup:', list_sort_over_index, end="\n\n")
我得到的错误是:
第 57 行,在
index_ratios = [round(index_times[i]/index_times[i-1], 2) for i in range(1, len(index_times)
我得到的完整错误是:
运行文件('C:/Users/sanil/OneDrive/Desktop/Business App Developement/hw9/turnin/hw09.py.py', wdir='C:/Users/sanil/OneDrive/Desktop/Business App Developement/hw9/turnin', current_namespace=True)
查字典次数:[0.0, 0.0, 0.0, 0.0]
列表查找次数:[0.0, 0.0, 0.0, 0.03130626678466797]
回溯(最近调用最后):
文件“C:\Users\sanil\OneDrive\Desktop\Business App Developement\hw9\turnin\hw09.py.py”,第 57 行,在
index_ratios = [round(index_times[i]/index_times[i-1], 2) for i in range(1, len(index_times))]
文件“C:\Users\sanil\OneDrive\Desktop\Business App Developement\hw9\turnin\hw09.py.py”,第 57 行,在
index_ratios = [round(index_times[i]/index_times[i-1], 2) for i in range(1, len(index_times))]
ZeroDivisionError: 浮点除以零
我在 sypder 中 运行 宁此代码,我是否遗漏了应用程序中的任何插件,无论我 运行 代码我得到多少次相同的错误
尝试使用 time.perf_counter()
,将代码中 time.time()
的所有实例替换为 time.perf_counter()
。
来自time.time()
documentation,
[n]ote that even though the time is always returned as a floating
point number, not all systems provide time with a better precision
than 1 second.
这可能解释了为什么在非常快的操作中减去低精度时间时得到 0。
当我尝试 运行 这段代码时,我应该获取程序定位的时间值 [42] 但我一直收到错误
import random
import time
ranges = [1000, 10000, 100000, 1000000]
dict_times = []
index_times = []
sort_times = []
for i in ranges:
L = random.sample(range(i), i)
d = dict(zip(L,L))
#time for dict ------
#start the time here
start = time.time()
v = d[42]
#end the time here
end = time.time()
dict_times.append(end-start)
#time for index ------
#start the time here
start = time.time()
ind = L.index(42)
#end the time here
end = time.time()
index_times.append(end-start)
#time for sort -----
#start the time here
start = time.time()
L = sorted(L)
#end the time here
end = time.time()
sort_times.append(end-start)
print('Times for dictionary lookup:', dict_times, end="\n\n")
print('Times for list lookup:', index_times)
index_ratios = [round(index_times[i]/index_times[i-1], 2) for i in range(1, len(index_times))]
print('List lookup Ratios:', index_ratios, end="\n\n")
print('Times for list sort:', sort_times)
sort_ratios = [round(sort_times[i]/sort_times[i-1], 2) for i in range(1, len(sort_times))]
print('List sort Ratios:', sort_ratios, end="\n\n")
list_sort_over_index = [round(sort_times[i]/index_times[i], 2) for i in range(len(sort_times))]
print('List sort over list lookup:', list_sort_over_index, end="\n\n")
我得到的错误是: 第 57 行,在 index_ratios = [round(index_times[i]/index_times[i-1], 2) for i in range(1, len(index_times) 我得到的完整错误是:
运行文件('C:/Users/sanil/OneDrive/Desktop/Business App Developement/hw9/turnin/hw09.py.py', wdir='C:/Users/sanil/OneDrive/Desktop/Business App Developement/hw9/turnin', current_namespace=True) 查字典次数:[0.0, 0.0, 0.0, 0.0]
列表查找次数:[0.0, 0.0, 0.0, 0.03130626678466797] 回溯(最近调用最后):
文件“C:\Users\sanil\OneDrive\Desktop\Business App Developement\hw9\turnin\hw09.py.py”,第 57 行,在 index_ratios = [round(index_times[i]/index_times[i-1], 2) for i in range(1, len(index_times))]
文件“C:\Users\sanil\OneDrive\Desktop\Business App Developement\hw9\turnin\hw09.py.py”,第 57 行,在 index_ratios = [round(index_times[i]/index_times[i-1], 2) for i in range(1, len(index_times))]
ZeroDivisionError: 浮点除以零 我在 sypder 中 运行 宁此代码,我是否遗漏了应用程序中的任何插件,无论我 运行 代码我得到多少次相同的错误
尝试使用 time.perf_counter()
,将代码中 time.time()
的所有实例替换为 time.perf_counter()
。
来自time.time()
documentation,
[n]ote that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second.
这可能解释了为什么在非常快的操作中减去低精度时间时得到 0。