This repository was archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtimer.py
More file actions
61 lines (46 loc) · 1.49 KB
/
timer.py
File metadata and controls
61 lines (46 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# This file is part of the pyMor project (http://www.pymor.org).
# Copyright Holders: Felix Albrecht, Rene Milk, Stephan Rave
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
from __future__ import absolute_import, division, print_function
import time
import functools
class Timer(object):
'''You can use me as a context manager, plain instance or decorator to time execution
of a code scope::
with Timer() as timer:
do_some_stuff()
do more stuff()
#outputs time in (s)
### OR ###
@timing.Timer('name', logging.debug)
def function(*args):
do_stuff
function(1)
#outputs time in (s) to logging.debug
### OR ###
timer = timing.Timer()
timer.start()
do_stuff()
timer.stop()
print(timer.dt)
'''
def __init__(self, section):
self._section = section
self._start = 0
def start(self):
self.dt = -1
self._start = time.clock()
def stop(self):
self.dt = time.clock() - self._start
def __enter__(self):
self.start()
def __exit__(self, type_, value, traceback):
self.stop()
print('Execution of %s took %f (s)'%(self._section, self.dt))
def __call__(self, func):
func.decorated = self
@functools.wraps(func)
def new_func(*args, **kwargs):
with self:
return func(*args, **kwargs)
return new_func