| # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| class TimeProfile(object): |
| """Class for simple profiling of action, with logging of cost.""" |
| def __init__(self, description='operation'): |
| self._description = description |
| self._starttime = time.time() |
| """Returns the rounded delta. |
| Also stops the timer if Stop() has not already been called. |
| if self._endtime is None: |
| delta = self._endtime - self._starttime |
| delta = round(delta, 2) if delta < 10 else round(delta, 1) |
| logging.info('%s seconds to perform %s', self.GetDelta(), self._description) |
| def Stop(self, log=True): |
| log: Log the delta (defaults to true). |
| self._endtime = time.time() |