Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: added tic, tac, toc functions #83

Merged
merged 2 commits into from
Oct 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()