Timing Python Code
time.time()
To figure out what part of the code is taking the most time and/or to increase the speed of the code it is useful to time it. The lines/functions that take the most time are usually in loops. time.time() is simple, straightforward and accurate enough in my experience for large timescales(>0.5s). (It is not the exact computing time and affected by the other processes running on your computer.)
import time t_0 = [0.0, 0.0, 0.0] ### starting times t = [0.0, 0.0, 0.0] ### total times for i in array: t_0[0] = time.time() func0(i) t[0] = t[0] + (time.time() - t_0[0]) .. t_0[1] = time.time() func1(i) t[1] = t[1] + (time.time() - t_0[1]) .. t_0[2] = time.time() func2(i) t[2] = t[2] + (time.time() - t_0[2]) .. print("t0: ", t[0] ) ### values are in second as floats print("t1: ", t[1] ) print("t2: ", t[2] )
timeit
More elegant than time.time() but also not as simple to implement.
Some examples : https://www.geeksforgeeks.org/timeit-python-examples/
Official documentation : https://docs.python.org/3/library/timeit.html
An interesting example : https://freud.readthedocs.io/en/latest/gettingstarted/examples/examples/Benchmarking%20RDF%20against%20MDAnalysis.html#Timing-Functions
others
Also possible, a very simple way: print the current time in your script https://stackoverflow.com/questions/1557571/how-do-i-get-time-of-a-python-programs-execution