Skip to content

Commit

Permalink
Merge pull request #83 from albop/timing
Browse files Browse the repository at this point in the history
ENH: added tic, tac, toc functions
  • Loading branch information
jstac committed Oct 16, 2014
2 parents f23a4b5 + c4d2ea0 commit 9e62b84
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
32 changes: 32 additions & 0 deletions quantecon/tests/test_timing.py
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()
81 changes: 81 additions & 0 deletions quantecon/timing.py
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()

0 comments on commit 9e62b84

Please sign in to comment.