We can all agree that IPython's %timeit magic is great for interactive use but what if you need to use the timeit output?
If you are using the magic command that means that you will have to manually assign the result to a variable. However the %timeit magic makes use of the timeit module to do the timing and you can do the same to capture the raw data.
The HTML table library can be found at this github repository.
def test():
c=0
for i in range(100):
c=i-c
return c
print(test())
50
First the magic!
%timeit test()
100000 loops, best of 3: 3.89 us per loop
There are two basic ways to use the timeit module. You can use the submodule timeit or the repeate submodule. That is to say timeit.timeit or timeit.repeat (confusing).
This is the form of timeit.timeit, including parameters.
timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000)
What is returned is the total time it take to execute the stmt command the number of times specified (in seconds).
The form of timeit.repeat is:
timeit.repeat(stmt='pass', setup='pass', timer=<default timer>, repeat=3, number=1000000)
What is returned is a list of the results of the timeit executions.
from timeit import Timer, timeit, repeat
print(__name__)
TimeIt = timeit("test()", setup="from __main__ import test", number=100000)
Repeat = repeat("test()", setup="from __main__ import test", repeat=3, number=100000)
print(TimeIt)
print(Repeat, min(Repeat))
__main__ 0.40849900245666504 [0.3967599868774414, 0.40363001823425293, 0.39429688453674316] 0.39429688453674316
As you can see there may be a little overhead taken into account with the %timeit magic, but we are really close.
If you have to time something programatically then you will need to use the timeit module. Otherwise, enjoy the simplicity of magic!
This section was inspired by this notebook by Liso77 and was posterd here.
Let's make a couple new functions to test and add a decorators to them.
from numpy import random, sqrt
func_list=[]
def decorate(func):
"""This function populates the func_list with lists of function names and the functions themselves.
The benifit of this is that is allow you to execute the functions just be itterating over them in a loop.
Aside from populating the func_list this decorator has no affect on the function's behaviour.*"""
global func_list
func_list += [[func.__name__, func]]
return func
@decorate
def add(data):
result=0
for i, j in [line.split(',') for line in data]:
result+= float(i)+float(j)
return result
@decorate
def mult(data):
result=0
for i, j in [line.split(',') for line in data]:
result= result+float(i)*float(j)
return result
@decorate
def dist(data):
result=0
for i, j in [line.split(',') for line in data]:
result= result+float(i)**2+float(j)**2
return sqrt(result)
def pick2(a,b): #This is used to validate the passing of multiple variables to the Repeat_time function
return a+b
list_size = (412,2) #sample data size
numbers = random.random_sample(size=list_size)
larger_data_text = ''
for x,y in numbers:
larger_data_text += "{:.1f}, {:.1f}\n".format(x,y)
larger_data_text = larger_data_text[:-1]
larger_data = larger_data_text.splitlines()
print(larger_data[0:10]+['...']) #Print the first 10 elements to see is the format is right.
#global data #not sure if this is needed
data = list(larger_data)
['0.7, 0.8', '0.6, 0.6', '0.8, 0.7', '0.1, 0.3', '0.8, 0.8', '0.9, 0.2', '0.3, 0.4', '0.2, 0.8', '0.2, 0.7', '0.3, 0.0', '...']
print(add(data))
print(mult(data))
print(dist(data))
403.7 102.34000000000003 16.3520640899
from IPython.core.display import HTML
from TimeIt_Func import testing
HTML(testing(func_list, data))
Function | Best time of 3 | Loops |
add | 519.643 us | 1000 |
mult | 520.225 us | 1000 |
dist | 670.213 us | 1000 |
%timeit add(data)
1000 loops, best of 3: 508 us per loop
%timeit mult(data)
1000 loops, best of 3: 514 us per loop
%timeit dist(data)
1000 loops, best of 3: 635 us per loop
for func in func_list: #Copied from Liso77's notebook with only minor modification
print(func[0])
%timeit func[1](data)
print('\n')
add 1000 loops, best of 3: 511 us per loop mult 1000 loops, best of 3: 512 us per loop dist 1000 loops, best of 3: 632 us per loop
from TimeIt_Func import Repeat_time
print(Repeat_time('add',"data"))
print(Repeat_time('pick2',"1, 2"))
('The best of 3 time is 0.521x10^-3 seconds for 1000 trials.', 0.0005211148262023926, 1000) ('The best of 3 time is 1.017x10^-7 seconds for 10000000 trials.', 1.016510009765625e-07, 10000000)
from TimeIt_Func import time_func
print(time_func("add","data",", data",1000))
[0.5092689990997314, 0.5073859691619873, 0.5081069469451904]
#print(globals().keys()) #Prints out everything in the current namespace.
Anyone is free to modify, copy, and/or extend the libraries and/or this notebook.