Python: Creating a function to time the execution of each of the algorithms
本问题已经有最佳答案,请猛点这里访问。
三种不同的算法定义如下:
1 2 3 4 5 6 7 8 | def alg1 (a, b) #body def alg2 (c, d) #body def alg3 (e, f) #body |
我们希望时间函数执行以下操作:
1 2 3 4 5 6 7 8 9 | def timefunc (s1, s2) #Start the clock #Call one of your algorithms #Stop the clock #Print the answer and time it took |
我这样做了,但没用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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)) |
请告诉我我做错了什么,或者你是否有更好的方法来做这件事。谢谢您。
您可以使用来自