The time.process_time()
method returns the time in seconds used by a process since its start.
The function measures the time spent on CPU-related tasks, such as calculations and data transfers, but does not include sleep times durations such as the time taken for waiting for input, output, or by the time.sleep()
function. This can be useful in evaluating the efficiency of different algorithms and to optimize the execution time of the program.
process_time()
The function does not take any argument.
It returns a floating point value representing the number of seconds elapsed since the start of the process
import time
print(time.process_time())
0.515625
In the above case, the return value 0.515625
indicates that the process has used 0.515625
seconds of CPU time.
Let us see an example with a process which takes a little more time.
import time
for i in range(5000):
i * 2
i ** 2
print("process time: ", time.process_time())
process time: 2.265625
In the above example, we used loops in order to use more processing time without requiring the loop outputs.
The process_time_ns() Function
The process_time_ns()
function works just like the process_time()
function, the only difference is that it returns the time taken by a process in nanoseconds instead of seconds.
time.process_time_ns()
import time
print(time.process_time_ns())
The time.perf_counter() function is similar to the process_time()
function except that it measures sleep durations as well.