# Copyright (c) Facebook, Inc. and its affiliates.
import datetime
import time
[ドキュメント]
class Timer(object):
def __init__(self):
self.reset()
@property
def average_time(self):
return self.total_time / self.calls if self.calls > 0 else 0.0
[ドキュメント]
def tic(self):
# using time.time instead of time.clock because time time.clock
# does not normalize for multithreading
self.start_time = time.time()
[ドキュメント]
def toc(self, average=True):
self.add(time.time() - self.start_time)
if average:
return self.average_time
else:
return self.diff
[ドキュメント]
def add(self, time_diff):
self.diff = time_diff
self.total_time += self.diff
self.calls += 1
[ドキュメント]
def reset(self):
self.total_time = 0.0
self.calls = 0
self.start_time = 0.0
self.diff = 0.0
[ドキュメント]
def avg_time_str(self):
time_str = str(datetime.timedelta(seconds=self.average_time))
return time_str
[ドキュメント]
def get_time_str(time_diff):
time_str = str(datetime.timedelta(seconds=time_diff))
return time_str