time.process_time() function in Python
Last Updated :
03 Jul, 2024
time.process_time()
function always returns the float value of time in seconds. Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process. It does not include time elapsed during sleep. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid. As
time module
provides various time-related functions. So it is necessary to import the time module otherwise it will through error because of the definition of
time.process_time()
is present in
time module
.
Example:
Understand the usage of the process_time().
Python3 1==
# Python program to show time by process_time()
from time import process_time
# assigning n = 50
n = 50
# Start the stopwatch / counter
t1_start = process_time()
for i in range(n):
print(i, end =' ')
print()
# Stop the stopwatch / counter
t1_stop = process_time()
print("Elapsed time:", t1_stop, t1_start)
print("Elapsed time during the whole program in seconds:",
t1_stop-t1_start)
Output:

process_time_ns():
It always gives the integer value of time in nanoseconds. Similar to process_time() but return time as nanoseconds.This is only the basic difference.
Example:
Understand the usage of the
process_time_ns()
.
Python3 1==
# Python program to show time by process_time_ns()
from time import process_time_ns
n = 50
# Start the stopwatch / counter
t1_start = process_time_ns()
for i in range(n):
print(i, end =' ')
print()
# Stop the stopwatch / counter
t1_stop = process_time_ns()
print("Elapsed time:", t1_stop, t1_start)
print("Elapsed time during the whole program in nanoseconds:",
t1_stop-t1_start)
Output:

Note:
process_time()
is very different from
pref_counter()
, as
perf_counter()
calculates the program time with sleep time and if any interrupt is there but
process_counter
only calculates the system and the CPU time during the process it not includes the sleep time.
Advantages of process_time() :
1. process_time() provides the system and user CPU time of the current process. 2. We can calculate float and integer both values of time in seconds and nanoseconds. 3. Used whenever there is a need to calculate the time taken by the CPU for the particular process.
time.process_time() function in Python – FAQs
What is process_time in Python?
process_time returns the CPU time used by the current process, excluding time spent on sleep or I/O operations.
Example:
import time
start_time = time.process_time()
# Code to measure
end_time = time.process_time()
print(f"CPU time used: {end_time - start_time} seconds")
What is the time() function in Python?
The time() function returns the current time in seconds since the Unix epoch (January 1, 1970).
Example:
import time
current_time = time.time()
print(f"Current time: {current_time} seconds since the Unix epoch")
How to check the time of a process in Python?
You can use time.process_time() for CPU time or time.time() for wall-clock time to measure the duration of code execution.
Example with process_time():
import time
start_time = time.process_time()
# Code to measure
end_time = time.process_time()
print(f"CPU time used: {end_time - start_time} seconds")
Example with time():
import time
start_time = time.time()
# Code to measure
end_time = time.time()
print(f"Elapsed time: {end_time - start_time} seconds")
What is time.sleep() in Python?
time.sleep() pauses the execution of the program for a specified number of seconds.
Example:
import time
print("Waiting for 3 seconds...")
time.sleep(3) # Pause execution for 3 seconds
print("3 seconds have passed")
Please Login to comment...