import time
def average(numbers):
"Return the average (arithmetic mean) of a sequence of numbers."
return sum(numbers) / float(len(numbers))
#example function
def domath(a,b,c):
a+b+c
a*b*c
a/b/c
a-b-c
a^b
b^c
def timedcalls(n, fn, *args):
times=[]
if type(n)==int:
t0 = time.clock()
for rep in range(n):
t0 = time.clock()
fn(*args)
t1 = time.clock()
times.append(t1-t0)
else:
start=time.clock()
while time.clock-start<int(n):
t0 = time.clock()
fn(*args)
t1 = time.clock()
times.append(t1-t0)
return min(times), average(times), max(times)
print timedcalls(5.0, domath, 1,2,3)
这段代码可以很好地处理int类型,但是由于某种原因,如果我使用float,它会给我这个错误。在
^{pr2}$这条线:
return min(times), average(times), max(times)
应该是
^{pr2}$您没有调用
time.clock
函数注意在回溯中,它说它不能在
'builtin_function_or_method' and 'float'
之间做减法相关问题 更多 >
编程相关推荐