How to find running time of a thread in Python
In the python documentation there is no mention of "thread timing". Either the clocks are process-wide or system-wide. In particular time.clock
measures process time while time.time
returns the system time.
In python3.3 the timings API was revised and improved but still, I can't see any timer that would return the process time taken by a single thread.
Also note that even if possible it's not at all easy to write such a timer. Timers are OS specific, so you would have to write a different version of the module for every OS. If you want to profile a specific action, just launch it without threads. When threaded the timing either it runs as expected, or it is a lot slower because of the OS, in which case you can't do nothing about it(at least, if you don't want to write a patch that "fixes" the GIL or removes it safely).
Python has progressed in the 6 years since this question was asked, and in version 3.3 it's introduced a tool for exactly what was being asked for here:
time.clock_gettime(time.CLOCK_THREAD_CPUTIME_ID)
Python 3.7 additionally introduced an analogous time.clock_gettime_ns
.
Detailed docs are exactly where you'd expect but the feature is pretty straightforward straight out of the box.
Python 3.7 has added the time.thread_time()
method that seems to do what this question needs. According to the docs, it is thread-specific and excludes time spent sleeping.