So in Java, we can do How to measure time taken by a function to execute
But how is it done in python? To measure the time start and end time between lines of codes? Something that does this:
import some_time_library
starttime = some_time_library.some_module()
code_tobe_measured()
endtime = some_time_library.some_module()
time_taken = endtime - starttime
With a help of a small convenience class, you can measure time spent in indented lines like this:
Which will show the following after the indented line(s) finishes executing:
The code for above class:
You could then name the code blocks you want to measure:
And nest them:
You can also use
import time
library:You can use
time.clock
for that.First call turns the timer on, and second call tells how many seconds has elapsed.
There are better profiling tools like
timeit
andprofile
, however this one will measure the time and this is what your are asking about