- testing the timeit function to measure the execution time of a line of print code;
- using python lists to record data;
- generate lists using the range function;
- multiplying a number with an entire list of numbers;
- how to use the bokeh module with CustomJSHover and HoverTool
This module provides a simple way to time small bits of Python code. It has both a Command-Line Interface as well as a callable one. It avoids a number of common traps for measuring execution times.
C:\Python373>python.exe
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Inte
l)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> import random
>>> timeit.timeit('print("a")',number=1)
a
0.00013114200010022614
>>> timeit.timeit('print("a")',number=100)
...
a
0.007177434000027461
>>> timeit.timeit('print("a")',number=1000)
...
a
0.07585798000002342
Let make more visible the results with the bokeh python module:import timeit
import random
# if you want to use numpy
#import numpy as np
# import bokeh python module
from bokeh.plotting import figure, show, output_file
# for show values
from bokeh.models.tools import CustomJSHover
from bokeh.models import HoverTool
print ('''
You can tests a number of times named l
with a number of print named i
and see the result value with bokeh python module
timeit.timeit('print("a")',number = i)
''')
i = int(input('The increment number i:'))
l = int(input('The number of test l:'))
# create the list with the output values
timit_list = []
# create the test function for l and i and return the list
def get_timeit(i,l):
while i < l:
i += 1
out=timeit.timeit('print("a")',number = i)
timit_list.append(out)
return timit_list
# run the test function with l and i
# this will be coordinate y
yt=get_timeit(i,l)
# show result of the test
print(yt)
# create the coordinate x
xt = [i for i in range(0, len(yt))]
#xt = np.linspace(0, l, l)
# print the coordinate x
#print(xt)
# create the output HTML file to see the result
output_file("test.html")
# create a figure with a timeit type y-axis
fig = figure(title='timit_list values for print("a")',
plot_height=400, plot_width=700,
x_axis_label='x has each incrementation of i', y_axis_label='value of timeit of print the char a',
x_minor_ticks=3,
toolbar_location=None)
# create a circle for each value
# see new multiplication with a list
# y=[i * 100 for i in yt]
fig.circle(x=xt, y=[i * 100 for i in yt],
color='blue', size=5,
legend='Values')
x_custom = CustomJSHover(code="""
return '' + special_vars.data_x""")
y_custom = CustomJSHover(code="""
return '' + special_vars.data_y""")
fig.add_tools(
HoverTool(
show_arrow=True,
tooltips=[
('xt', '$data_x'),
('yt', '$data_y')
],
formatters=dict(
xt=x_custom,
yt=y_custom
)
)
)
# Put the legend in the upper left corner
fig.legend.location = 'top_left'
# Let's check it out
show(fig)
The result of this python source code can be found on my YouTube channel: