We can use two modules in python to do this measure, the first one is time() and the second one is timeit().
- The time() module can be used for any code of any length, the usage is as follow :
from time import time # first we should import the module
start = time() # then we set the start of measure
def function(n) : #then we put our code
return " ".join(str(n) for n in range(100))
end = time() # finally we set the end of measure
print(end-start) # we print the measured time
- the timeit() module is perfect for small snipets of code, but it is not practical for large code, the implementation is as follows :
After importing the module, now we use the method timeit() that takes two arguments the first one is the code snippet between ' ' and the second argument is the number of ? .
import timeit
print(timeit.timeit('" ".join(str(n) for n in range(1000000))', number = 10000))
I hope that you found this useful.
Buy Website Traffic
0 Comments