User Tools

Site Tools


coding:timing_code

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

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

coding/timing_code.txt · Last modified: 2021/05/14 13:11 by bargun2

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki