三种不同的算法定义如下:
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))
请让我知道我做错了什么,或者你有更好的方法。谢谢。
答案 0 :(得分:1)
您可以使用class Test {
String internal = 'a,b,c'
List getList() {
return internal .split(',')
}
void setList(List list) {
internal = list.join(',')
}
}
def t = new Test()
println t.internal // a,b,c
println t.list // [a, b, c]
t.list << 'd' // this does not work! does not add new element
println t.list // [a, b, c]
t.list = t.list << 'd' // work-around that works
println t.list // [a, b, c, d]
模块中的time.time()
。