testFunctions.py
1.07 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
def tic(tag=None):
"""
Start timer function.
tag = used to link a tic to a later toc. Can be any dictionary-able key.
"""
global TIC_TIME
if tag is None:
tag = 'default'
try:
TIC_TIME[tag] = time.time()
except NameError:
TIC_TIME = {tag: time.time()}
def toc(tag=None, save=False, fmt=False):
"""
Timer ending function.
tag - used to link a toc to a previous tic. Allows multipler timers, nesting timers.
save - if True, returns int time to out (in ms)
fmt - if True, formats time in H:M:S, if False just seconds.
"""
global TOC_TIME
template = 'Elapsed time is:'
if tag is None:
tag = 'default'
else:
template = u'%s - '%tag + template
try:
TOC_TIME[tag] = time.time()
except NameError:
TOC_TIME = {tag: time.time()}
if TIC_TIME:
d = int((TOC_TIME[tag]-TIC_TIME[tag]) * 1000)
if save: return d
else:
print("no tic() start time available. Check global var settings")