2011-08-08 16 views
2

python에서 timeit 모듈에 대한 질문이 있습니다.이 모듈은 코드 조각을 실행하는 데 걸리는 시간을 결정하는 데 사용됩니다.python의 timeit 모듈에 대한 질문

t = timeit.Timer("foo","from __main__ import foo") 
str(t.timeit(1000)) 

위의 코드에서 인수 1000의 의미는 무엇입니까?

답변

1

documented으로,이 숫자는 timeit이 지정된 프로그램을 실행해야하는 횟수를 나타냅니다.

캐싱으로 인해 처음 몇 번의 실행이 현저히 느려질 수 있고 개별 실행 시간이 크게 달라질 수 있으므로 더 많은 타이밍 실행 (즉, 더 높은 값)은 더 정확한 결과를 산출 할뿐만 아니라 더 오래 걸립니다. documentation에서

4

:

Timer.timeit([number=1000000]) 

시간 주 문의 number 실행. 이렇게하면 설치 문을 한 번 실행 한 다음실행에 걸리는 시간을 초 단위로 여러 번 플로트로 반환합니다. 인수는 루프를 통한 횟수이며, 기본값은 백만입니다. main 문, setup 문 및 타이머 함수를 사용하여 생성자에 전달합니다.

물고기 사람을 가르치는 정신에서
1

, 파이썬을 물어

>>> import timeit 
>>> t=timeit.Timer() 
>>> help(t.timeit) 
Help on method timeit in module timeit: 

timeit(self, number=1000000) method of timeit.Timer instance 
    Time 'number' executions of the main statement. 

    To be precise, this executes the setup statement once, and 
    then returns the time it takes to execute the main statement 
    a number of times, as a float measured in seconds. The 
    argument is the number of times through the loop, defaulting 
    to one million. The main statement, the setup statement and 
    the timer function to be used are passed to the constructor. 
관련 문제