In [1]:
import stacklog
import time
def comp(t=.1):
time.sleep(t)
The Logger accumulates input, but we can make sure we have an empty log with reset.
In [2]:
stacklog.reset()
tic() and toc() can be used to time a section of codetic() needs a key to identify the result in a dictionarylog() is an interface to the dictionary of all the Logger objectslog() returns the default Logger (same as log('default'))
In [13]:
from stacklog import tic, toc, log, reset
reset()
# normal tic and toc for timing
tic('test1')
comp()
toc()
# with the same key, times get appended to a list
tic('multi')
comp()
toc()
tic('multi')
comp()
toc()
tic('multi')
comp()
toc()
# pretty print the result
log()
Out[13]:
Logger contains a dictionary of all recorded timingspeek exposes that dictionary without reseting itpull exposes the dictionary and reset the Loggerpretty_dict will attemp to pretty print the dictionarypretty_dict's clean argument defaults to True, which will remove empty data structures
and replace single-element lists with their value
In [8]:
from stacklog import peek, pull, pretty_dict
# grab the dictionary of the log
result = peek()
# print the full data structure
pretty_dict(result, clean=False)
print "#"*10
# pretty print a cleaned up version (removing empty containers and extra parens)
pretty_dict(result)
timer context manager and the with statement instead of tic and toc
In [9]:
from stacklog import timer
# can also use a context manager to time instead of tic and toc
with timer('something new'):
comp()
log()
Out[9]:
pull returns the Logger's dictionary and resets the log
In [11]:
result1 = pull()
pretty_dict(result1)
print "#"*10
with timer('all alone'):
comp()
peek()
Out[11]:
In [15]:
reset()
with timer('outer'):
tic('inner')
comp()
toc()
with timer('another inner'):
comp()
comp()
comp()
for i in range(4):
with timer('way in there'):
comp()
pretty_dict(peek())
lost_time to see the difference between a parent timer and the sum of its children to see how much time is unaccounted for
In [19]:
from stacklog import lost_time
lost_time(peek())
Out[19]:
pretty_dict with the output of lost_time
In [20]:
pretty_dict(lost_time(peek()))
comp() functions should take to execute
In [21]:
reset()
with timer('outer'):
tic('inner')
comp()
toc()
with timer('another inner'):
with timer('previously unaccounted for'):
comp()
comp()
comp()
for i in range(4):
with timer('way in there'):
comp()
pretty_dict(peek())
In [22]:
pretty_dict(lost_time(peek()))
In [3]:
from stacklog import Logger
a = Logger()
b = Logger()
a.tic('outer')
comp()
b.tic('something else')
comp()
comp()
comp()
a.toc()
comp()
comp()
b.toc()
print a
print b