"""
This is my wrapper for the time module. There's probably an
easier way to time the duration of things, but when I looked
into timing stuff, this was the best I could come up with...
To use:
t = Stopwatch()
t.start_time()
# do something
elapsed = t.finish()
"""
import time
class Stopwatch:
"""
Creates stopwatch timer objects.
"""
# stores the time the stopwatch was started
t0 = None
# stores the time the stopwatch was last looked at
t1 = None
def __init__(self):
self.t0 = 0
self.t1 = 0
def start(self):
"""
Stores the current time in t0.
"""
self.t0 = time.time()
def finish(self):
"""
Returns the elapsed duration in milliseconds. This
stores the current time in t1, and calculates the
difference between t0 (the stored start time) and
t1, so if you call this multiple times, you'll get a
larger answer each time.
You have to call this in order to update t1.
"""
self.t1 = time.time()
return self.milli()
def milli(self):
"""
Returns t1 - t0 in milliseconds. Does not update t1.
"""
return int((self.t1 - self.t0) * 1000)
Sunday, August 3, 2008
Timing how long things are taking
When I first started writing experiments in PyEPL, I had a lot of trouble getting the timing right. I wanted a way to time how long things were taking that didn't make use of the PyEPL PresentationClock, to confirm that all was well. The Stopwatch class below is based heavily on timing-example-2.py.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment