-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpytictoc.py
77 lines (60 loc) · 2.55 KB
/
pytictoc.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""
Module with class TicToc to replicate the functionality of MATLAB's tic and toc.
Documentation: https://pypi.python.org/pypi/pytictoc
"""
__author__ = 'Eric Fields'
__version__ = '1.5.3'
__version_date__ = '2 August 2023'
from timeit import default_timer
class TicToc(object):
"""
Replicate the functionality of MATLAB's tic and toc.
#Methods
TicToc.tic() #start or re-start the timer
TicToc.toc() #print elapsed time since timer start
TicToc.tocvalue() #return floating point value of elapsed time since timer start
#Attributes
TicToc.start #Time from timeit.default_timer() when t.tic() was last called
TicToc.end #Time from timeit.default_timer() when t.toc() or t.tocvalue() was last called
TicToc.elapsed #t.end - t.start; i.e., time elapsed from t.start when t.toc() or t.tocvalue() was last called
"""
def __init__(self):
"""Create instance of TicToc class."""
self.start = float('nan')
self.end = float('nan')
self.elapsed = float('nan')
def tic(self):
"""Start the timer."""
self.start = default_timer()
def toc(self, msg='Elapsed time is', restart=False):
"""
Report time elapsed since last call to tic().
Optional arguments:
msg - String to replace default message of 'Elapsed time is'
restart - Boolean specifying whether to restart the timer
"""
self.end = default_timer()
self.elapsed = self.end - self.start
print('%s %f seconds.' % (msg, self.elapsed))
if restart:
self.start = default_timer()
def tocvalue(self, restart=False):
"""
Return time elapsed since last call to tic().
Optional argument:
restart - Boolean specifying whether to restart the timer
"""
self.end = default_timer()
self.elapsed = self.end - self.start
if restart:
self.start = default_timer()
return self.elapsed
def __enter__(self):
"""Start the timer when using TicToc in a context manager."""
self.start = default_timer()
def __exit__(self, *args):
"""On exit, pring time elapsed since entering context manager."""
self.end = default_timer()
self.elapsed = self.end - self.start
print('Elapsed time is %f seconds.' % self.elapsed)