Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit 4a4e7ab

Browse files
committed
Added a simple timing module.
1 parent e400458 commit 4a4e7ab

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

biosppy/timing.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
biosppy.timing
4+
--------------
5+
6+
This module provides simple methods to measure computation times.
7+
8+
:copyright: (c) 2015-2017 by Instituto de Telecomunicacoes
9+
:license: BSD 3-clause, see LICENSE for more details.
10+
"""
11+
12+
# Imports
13+
# compat
14+
from __future__ import absolute_import, division, print_function
15+
# from six.moves import map, range, zip
16+
# import six
17+
18+
# built-in
19+
import time
20+
21+
# 3rd party
22+
23+
# local
24+
25+
# Globals
26+
CLOCKS = dict()
27+
DFC = '__default_clock__'
28+
29+
30+
def tic(name=None):
31+
"""Start the clock.
32+
33+
Parameters
34+
----------
35+
name : str, optional
36+
Name of the clock; if None, uses the default name.
37+
38+
"""
39+
40+
if name is None:
41+
name = DFC
42+
43+
CLOCKS[name] = time.time()
44+
45+
46+
def tac(name=None):
47+
"""Stop the clock.
48+
49+
Parameters
50+
----------
51+
name : str, optional
52+
Name of the clock; if None, uses the default name.
53+
54+
Returns
55+
-------
56+
delta : float
57+
Elapsed time, in seconds.
58+
59+
Raises
60+
------
61+
KeyError if the name of the clock is unknown.
62+
63+
"""
64+
65+
toc = time.time()
66+
67+
if name is None:
68+
name = DFC
69+
70+
try:
71+
delta = toc - CLOCKS[name]
72+
except KeyError:
73+
raise KeyError('Unknown clock.')
74+
75+
return delta
76+
77+
78+
def clear(name=None):
79+
"""Clear the clock.
80+
81+
Parameters
82+
----------
83+
name : str, optional
84+
Name of the clock; if None, uses the default name.
85+
86+
"""
87+
88+
if name is None:
89+
name = DFC
90+
91+
CLOCKS.pop(name)
92+
93+
94+
def clear_all():
95+
"""Clear all clocks."""
96+
97+
CLOCKS.clear()

0 commit comments

Comments
 (0)