11.17. Timing your Code#
11.17.1. Approach 1#
use time library
import time
start = time.time() # tells time in second
"the code you want to test stays here"
end = time.time()
print(end - start)
7.510185241699219e-05
11.17.2. Approach 2#
use magic command
there are some magic commands in jupyter notebook which u can use to measure time
‘%time’
It is used if you want to measure time of one statement
If using %,write the statement just after you write %time
eg, %time [your statement] -‘%%time’
It is used if you want to measure time of whole cell
If using %%,don’t write anything on same line ie,code should start from next line
eg, %%time
[your code]
this can be used for above scenario as well if you write the statement in below line
11.17.3. Examples#
%time [i for i in range(1,10,2)]
CPU times: user 6 µs, sys: 0 ns, total: 6 µs
Wall time: 10 µs
[1, 3, 5, 7, 9]
%%time
l=[i for i in range(1,100000,2)]
l=l*22
CPU times: user 5.23 ms, sys: 3 ms, total: 8.23 ms
Wall time: 8.15 ms