본 포스팅에서는 파이썬으로 특정 코드의 실행시간을 제어하기 위한 delay함수 사용방법에 대해 알아보겠습니다.

 

먼저 time package를 install 해야합니다.

>>>pip install time

 

time package는 시간 관련 함수를 제공하는 패키지 (it provides various time-related functions) 입니다.

 

time delay를 주기 위해서는 time 패키지 내의 time.sleep 함수를 사용하면 됩니다.

 

time.sleep 함수의 용법은 다음과 같습니다.

 

Usage: time.sleep(secs)

-인자로 주어지는 시간 단위(초) 동안 실행을 중단합니다.

  Suspend execution of the calling thread for the given number of seconds.

 

- 좀 더 정확한 타이밍 제어를 위한다면 인자를 소수점 형태로도 줄 수 있습니다.

  The argument may be a floating point number to indicate a more precise sleep time.

 

- sleep() 함수 이후 실행되는 어떤 시그널의 catching routine으로 인해, 실제로 중단하는 시간은 요청 시간보다 짧을 수 있습니다.

  The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine.

 

- 시스템 상의 다른 테스크로 인해 실제로 중단하는 시간이 요청시간 보다 길 수 도 있습니다. 

  Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.

 


print 코드를 특정 시점에 실행하는 예제를 통해 어떻게 사용되는지 알아보겠습니다.

 

>>> import time
>>> print('hello', time.time( ) )   

hello , 0.001 sec                       # 'hello' print 되는 시각 확인

>>> time.sleep(5)                    # 5초 대기
>>> print('world', time.time( ) ) 

world, 5.001 sec                      #  'world' print되는 시각 확인 (5초 간격 확인)

 

*time.time()함수는 실행 속도를 측정하기 위한 time stamp 함수입니다.

+ Recent posts