2014-01-05 5 views
0

다음에서 설정 매개 변수가 잘못되었습니다. 다음과 같이중첩 된 함수를 timeit으로 가져 오기

  • setup="from __main__ import bubbleSort,from copy import copy"
  • setup="from __main__ import bubbleSort,copy"
  • setup="from __main__ import bubbleSort"

역 추적을 :

import timeit 
import random 
from copy import copy 

def shortBubbleSort(aList): 
    n = len(aList) - 1 
    iterating = True 
    while n > 0 and iterating: 
     iterating = False 
     for i in range(n): 
      if aList[i+1] < aList[i]: 
       iterating = True 
       aList[i], aList[i+1] = aList[i+1], aList[i] 
     n -= 1 
    return aList 


L = [] 
for i in range(1,500): 
    L.append(random.randrange(0,1000000)) 

x = timeit.repeat("bubbleSort(copy(L))", setup="from __main__ import bubbleSort,copy,L",repeat = 100,number = 100) 
y = (sum(x)/len(x))*100 
print(str(y)) 

은 또한 다음을 시도했습니다

Traceback (most recent call last): 
    File "C:\Users\Administrator\AppData\Local\ActiveState\KomodoEdit\7.1\samples\bubbleSort TimeIt.py", line 24, in <module> 
    x = timeit.repeat("bubbleSort(copy(L))", setup="from __main__ import bubbleSort,copy,L",repeat = 100,number = 100) 
    File "C:\Python32\lib\timeit.py", line 235, in repeat 
    return Timer(stmt, setup, timer).repeat(repeat, number) 
    File "C:\Python32\lib\timeit.py", line 223, in repeat 
    t = self.timeit(number) 
    File "C:\Python32\lib\timeit.py", line 195, in timeit 
    timing = self.inner(it, self.timer) 
    File "<timeit-src>", line 3, in inner 
ImportError: cannot import name bubbleSort 
+0

무엇이 오류입니까? – mgilson

+2

글쎄, 그게 뭔가 잘못되었다고 생각하니? 오류가 있습니까? 그렇다면 전체 추적을 보여주십시오. – delnan

+0

ok - 이제는 오타라고 알고 있지만 추가 할 예정입니다. – whytheq

답변

0

아마도 오타입니다. 실제 함수 이름이 shortBubbleSort이고 당신이 내 컴퓨터에

bubbleSort을 가져 오는 결과는 그것은 오타

192.437240362 
+0

+1 .... .... 부끄러운 모자는 어 딨지? – whytheq

0

했다. 귀하의 기능은 이 아니라 bubbleSort입니다. 작동 방식 :

x = timeit.repeat("shortBubbleSort(copy(L))", setup="from __main__ import shortBubbleSort,copy,L",repeat = 100,number = 100) 
관련 문제