我开发了两种算法来解决Euler项目的问题1,我试图确定哪种算法更快。因此,我决定根据此站点上的建议之一导入时间模块。运行时间约为1000秒。但是我仍然希望看到小数点后至少5位数字的运行时间。因此,我尝试了各种格式化方法,例如round(),repr(),str()(然后将字符串切成薄片),尝试使用format修饰符,但似乎无济于事。我什至会附上我的代码以取得良好效果。请提出对我有帮助的修改!
import time
start_time = time.time()
check1 = True
x = 0
sum = 0
while check1:
x += 3
sum += x
if x >=1000:
check1 = False
y = 0
check2 = True
while check2:
y += 5
if x % 3 != 0:
sum += y
if y >= 1000:
check2 = False
print(sum)
print(round(time.time() - start.time , 5))
只是不介意缩进,当我复制代码时可能出了点问题!我也添加了输出的屏幕截图。
您在应用字符串格式方面犯了一个错误。尝试这个:
print('The running time is %.5f'%(time.time()-start_time))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句