cvtools.utils.timer 源代码

# Copyright (c) 2017-present, Facebook, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
#
# Based on:
# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------

"""Timing related functions."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import time
import datetime


[文档]class Timer(object): """A simple timer.""" def __init__(self): self.reset()
[文档] 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.diff = time.time() - self.start_time self.total_time += self.diff self.calls += 1 self.average_time = self.total_time / self.calls if average: return self.average_time else: return self.diff
[文档] def reset(self): self.total_time = 0. self.calls = 0 self.start_time = 0. self.diff = 0. self.average_time = 0.
[文档]def get_time_str(form='%Y%m%d_%H%M%S'): """for example form='%Y%m%d_%H%M%S_%f'""" # return time.strftime(form, time.localtime()) return datetime.datetime.now().strftime(form)
[文档]def get_timestamp_str(): return str(int(round(time.time() * 1000000)))
[文档]def get_human_time(): # TODO: 人类友好的时间字符串 pass