Open In App

time.process_time() function in Python

Last Updated : 08 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

time.process_time() is a function in Python’s time module that returns the CPU time used by the current process as a floating-point number in seconds. Unlike time.time(), which measures real (wall-clock) time, time.process_time() focuses on the time spent by the CPU executing the process, excluding time spent waiting for I/O operations or sleep time.

Python
import time

s = time.process_time() # start time

# perform some computations
sum = 0
for i in range(1, 1000000):
    sum += i

e = time.process_time() # end time

print(e - s, "seconds")

Output
0.127746878 seconds

Explanation: This code records CPU time s , sums numbers from 1 to 999,999 in a loop, then records the end time e. The difference e – s gives the CPU time used, ignoring idle or sleep time.

Syntax

import time

cpu_time = time.process_time()

Parameters: time.process_time() function does not take any parameters.

Return: Returns the CPU time used by the current process as a floating-point number in seconds.

Examples

Example 1: Measuring execution time of a function

Python
import time

def compute():
    return sum(range(1000000))

s = time.process_time() # start time
compute()
e = time.process_time() # end time

print(f"{e - s} seconds")

Output
0.024878582999999996 seconds

Explanation: This code records the start time s , runs compute() to sum numbers from 0 to 999,999, then records the end time e. The difference e – s gives the CPU time used, excluding idle or sleep time.

Example 2: Comparing performance of two approach

Python
import time  

# Measure built-in sum time
s = time.process_time()  # start time 
sum(range(1000000))  
e = time.process_time()  # end time
print(f"Built-in sum: {e - s} sec")  

# Measure loop sum time  
s = time.process_time()  # start time
t = 0  # store sum 

for i in range(1000000):  
    t += i  
e = time.process_time()  # end time
print(f"Loop sum: {e - s} sec")  

Output
Built-in sum: 0.035290503 sec
Loop sum: 0.223168331 sec

Explanation: This code records CPU time s , runs sum(range(1000000)), then records the end time e and prints the elapsed time. Next, it measures a manual loop summation, storing the sum in t. It records the start time s , runs the loop, then records the end time e.

Example 3: Measuring CPU time for multiple iterations

Python
import time  

s = time.process_time()  # start time  
for _ in range(5):  
    sum(range(1000000))  # run multiple times  
e = time.process_time()  # end time  

print(f"CPU Time: {e - s} sec")  

Output
CPU Time: 0.14665630300000002 sec

Explanation: This code measures CPU time for executing sum(range(1000000)) five times. It records the start time s , runs the summation inside a loop, then records the end time e. The difference e – s gives the total CPU time used.

Key features of time.process_time()

  • CPU-Specific Timing: Measures only the time during which the CPU was actively executing the process.
  • Independent of System Clock: Unaffected by changes in system time or delays due to I/O operations.
  • High Precision: Useful for profiling and benchmarking Python code.

Difference between time.process_time() and time.time()

Features

time.process_time()

time().time()

Measures

CPU execution time

Wall-clock time

Include Sleep time

No

Yes

Affected by system time changes

No

Yes

Suitable for

Benchmarking CPU-intensive tasks

Measuring real-world elapsed time



Next Article
Article Tags :
Practice Tags :

Similar Reads