-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from albop/timing
ENH: added tic, tac, toc functions
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Filename: timing.py | ||
Authors: Pablo Winant | ||
Tests for timing.py | ||
""" | ||
|
||
def test_tic_tac_toc(): | ||
|
||
from ..timing import tic, tac, toc | ||
import time | ||
|
||
h = 0.1 | ||
|
||
tic() | ||
|
||
time.sleep(h) | ||
el1 = tac() | ||
|
||
time.sleep(h) | ||
el2 = tac() | ||
|
||
time.sleep(h) | ||
el3 = toc() | ||
|
||
assert(abs(el1-h)<0.01) | ||
assert(abs(el2-h)<0.01) | ||
assert(abs(el3-h*3)<0.01) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
test_tic_tac_toc() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
""" | ||
Filename: timing.py | ||
Authors: Pablo Winant | ||
Date: 10/16/14 | ||
Provides Matlab-like tic, tac and toc functions. | ||
""" | ||
|
||
class __Timer__: | ||
'''Computes elapsed time, between tic, tac, and toc. | ||
Methods | ||
------- | ||
tic : | ||
Resets timer. | ||
toc : | ||
Returns and prints time elapsed since last tic(). | ||
tac : | ||
Returns and prints time elapsed since last | ||
tic(), tac() or toc() whichever occured last. | ||
''' | ||
|
||
start = None | ||
last = None | ||
|
||
def tic(self): | ||
"""Resets timer.""" | ||
|
||
|
||
import time | ||
|
||
t = time.time() | ||
self.start = t | ||
self.last = t | ||
|
||
|
||
def tac(self): | ||
"""Returns and prints time elapsed since last tic()""" | ||
|
||
import time | ||
|
||
if self.start is None: | ||
raise Exception("tac() without tic()") | ||
|
||
t = time.time() | ||
elapsed = t-self.last | ||
self.last = t | ||
|
||
print("TAC: Elapsed: {} seconds.".format(elapsed)) | ||
return elapsed | ||
|
||
|
||
def toc(self): | ||
"""Returns and prints time elapsed since last | ||
tic() or tac() whichever occured last""" | ||
|
||
import time | ||
|
||
if self.start is None: | ||
raise Exception("toc() without tic()") | ||
|
||
t = time.time() | ||
self.last = t | ||
elapsed = t-self.start | ||
|
||
|
||
print("TOC: Elapsed: {} seconds.".format(elapsed)) | ||
return elapsed | ||
|
||
__timer__ = __Timer__() | ||
|
||
def tic(): | ||
"""Saves time for future use with tac or toc.""" | ||
return __timer__.tic() | ||
|
||
def tac(): | ||
"""Prints and returns elapsed time since last tic, tac or toc.""" | ||
return __timer__.tac() | ||
|
||
def toc(): | ||
"""Prints and returns elapsed time since last tic, tac or toc.""" | ||
return __timer__.toc() |