The objective of this notebook is to set up some examples to understand the output of timeit and work towards having useful functions to be able to quickly study the performance of python functions, particularly when these functions have sequences as inputs, and as a function of sequence size.
A lot of this notebook is based on trying to understand material from
In [4]:
from __future__ import absolute_import, print_function, division
In [1]:
import timeit
import time
In [2]:
import numpy as np
import pandas
In [3]:
import functools
As explained in the python docs(https://docs.python.org/2/library/timeit.html), the timeit object works on two strings:
time.sleep()
function with a default sleepTime of 1 sec.
In [5]:
def statement(sleepTime=1, stmt=None):
"""
return a string that includes code to print out stmt and sleep for sleepTime.
Parameters
----------
sleepTime :
stmt :
Returns
-------
"""
if stmt is None:
stmt = 'print("running stmt"); time.sleep({})'.format(sleepTime)
return stmt
In [6]:
statement()
Out[6]:
In [7]:
def stp(sleepTime=3, setup=None):
if setup is None:
setup = 'print("running setup"); time.sleep({})'.format(sleepTime)
return setup
In [8]:
stp()
Out[8]:
In [11]:
tstart = time.time()
tt = timeit.Timer(stmt=statement(), setup=stp())
l = tt.timeit(number=2)
tend = time.time()
print('wall time elapsed from time.time()', tend - tstart)
print('timeit output: ', l)
So, this is what happened:
In [20]:
tstart = time.time()
l = timeit.timeit(stmt=statement(), setup=stp(), timer=timeit.default_timer, number=2)
tend = time.time()
print('wall time elapsed from time.time()', tend - tstart)
print('timeit output: ', l)
In [13]:
tstart = time.time()
x = np.array(tt.repeat(repeat=3, number=2) )
tend = time.time()
print(tend - tstart)
print(x)
So, unlike number, the repeat argument is a complete repeat of the run, running both setup and the statement. The return is in a list.
For timing a deterministic piece of computation performed by the statement code, one would expect the minimum value of the time to be the true value (all of the extra time coming from different levels of background processes running. So, ideally, I would like to be able to
In [24]:
def timemyfunc(func, args=None, setup='pass', number=3, repeat=3):
if args is None:
stmt = 'func()'
else:
stmt = 'func(*args)'
timeit.Timer(stmt=stmt, setup=setup)
res = np.asarray(timeit.repeat(number=number, repeat=repeat))
return res/number
In [53]:
def square(num, val):
x = np.arange(num)
print(val)
return x * x
In [51]:
timemyfunc(square, args=[5e10000000000], setup='import numpy as np', number=10000)
Out[51]:
In [52]:
import functools
In [56]:
functools.partial(square, val='time')(2)
Out[56]:
In [57]:
functools.partial(square, 5)('statement')
Out[57]:
In [58]:
functools.partial(square, num=5)
Out[58]:
In [ ]: