타이밍을 많이 사용한다면이 작은 (20 줄) 컨텍스트 관리자를 추천 할 수 있습니다.
코드는 다음과 같습니다.
#!/usr/bin/env python
# coding: utf-8
from timer import Timer
if __name__ == '__main__':
a, record = None, []
while not a == '':
with Timer() as t: # everything in the block will be timed
a = input('Type: ')
record.append(t.elapsed_s)
# drop the last item (makes a copy of the list):
record = record[:-1]
# or just delete it:
# del record[-1]
참고로 다음은 타이머 컨텍스트 관리자 의 내용입니다 .
from timeit import default_timer
class Timer(object):
""" A timer as a context manager. """
def __init__(self):
self.timer = default_timer
# measures wall clock time, not CPU time!
# On Unix systems, it corresponds to time.time
# On Windows systems, it corresponds to time.clock
def __enter__(self):
self.start = self.timer() # measure start time
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
self.end = self.timer() # measure end time
self.elapsed_s = self.end - self.start # elapsed time, in seconds
self.elapsed_ms = self.elapsed_s * 1000 # elapsed time, in milliseconds