【发布时间】:2018-04-15 02:08:43
【问题描述】:
三种不同的算法定义如下:
def alg1 (a, b)
#body
def alg2 (c, d)
#body
def alg3 (e, f)
#body
我们希望时间函数执行以下操作:
def timefunc (s1, s2)
#Start the clock
#Call one of your algorithms
#Stop the clock
#Print the answer and time it took
我这样做了,但它不起作用:
from datetime import datetime
def timefunc (s1, s2):
startTime1= datetime.now()
alg1(s1, s2)
timeElapsed1=datetime.now()-startTime1
print('Time elpased for alg1 '.format(timeElapsed1))
startTime2= datetime.now()
alg2(s1,s2)
timeElapsed2=datetime.now()-startTime2
print('Time elpased for alg2 '.format(timeElapsed2))
startTime3= datetime.now()
alg3(s1,s2)
timeElapsed3=datetime.now()-startTime3
print('Time elpased for alg3 '.format(timeElapsed3))
请让我知道我做错了什么,或者您是否有更好的方法来做到这一点。谢谢你。
【问题讨论】:
-
为什么要重新发明轮子而不是使用
timeit或time模块? -
怎么不工作了?它给出了什么输出?你期待什么输出?你应该发一个minimal reproducible example。
-
如果您至少可以向我们展示错误
-
使用
time.time()作为开始时间和结束时间