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