2014-01-21 3 views
0
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을 사용하면 어떤 이유로이 오류가 발생합니다. 왜 형식 오류가 발생합니까?

Traceback (most recent call last): 
    File "<stdin>", line 29, in <module> 
    File "<stdin>", line 22, in timedcalls 
TypeError: unsupported operand type(s) for -: 'builtin_function_or_method' and 'float' 

이 라인은 :

return min(times), average(times), max(times) 
+1

코드 형식을 수정하십시오. – inspectorG4dget

답변

7
while time.clock-start<int(n): 

while time.clock()-start<int(n): 

당신은 time.clock 함수를 호출하지 않을 수 있어야

역 추적에서 뺄셈을 할 수 없다는 메시지가 나타남 'builtin_function_or_method' and 'float'