Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
time.process_time() function in Python
The time.process_time() function in Python returns the sum of system and user CPU time consumed by the current process. Unlike time.time(), it excludes time spent sleeping and focuses only on actual processing time.
Syntax
time.process_time()
This function returns a float value representing the CPU time in seconds.
Basic Example
Here's how to get the current process time ?
import time
# Get current process time
current_time = time.process_time()
print(f"Current process time: {current_time} seconds")
Current process time: 0.015625 seconds
Measuring Execution Time
The most common use case is measuring how long a process takes to execute ?
import time
def print_numbers(limit):
for i in range(limit):
print(i, end=' ')
# Measure execution time
start_time = time.process_time()
print_numbers(100)
end_time = time.process_time()
print() # New line after numbers
print(f"Process time: {end_time - start_time:.6f} seconds")
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 Process time: 0.015625 seconds
Practical Example: CPU-Intensive Task
Let's measure the CPU time for a computation-heavy task ?
import time
def calculate_factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
# Measure CPU time for factorial calculation
start_time = time.process_time()
factorial_result = calculate_factorial(10000)
end_time = time.process_time()
print(f"Factorial calculated successfully!")
print(f"CPU time used: {end_time - start_time:.6f} seconds")
Factorial calculated successfully! CPU time used: 0.031250 seconds
Key Points
Understanding process_time() characteristics:
- CPU time only: Excludes time spent waiting or sleeping
- Process-specific: Measures only the current process
- Monotonic: Values always increase, never go backward
- System + User time: Includes both kernel and user space execution
Comparison with Other Time Functions
import time
# Compare different time functions
start_process = time.process_time()
start_perf = time.perf_counter()
# Simulate work with a small delay
for i in range(1000000):
pass
end_process = time.process_time()
end_perf = time.perf_counter()
print(f"process_time(): {end_process - start_process:.6f} seconds")
print(f"perf_counter(): {end_perf - start_perf:.6f} seconds")
process_time(): 0.078125 seconds perf_counter(): 0.082156 seconds
Conclusion
Use time.process_time() when you need to measure actual CPU processing time, excluding sleep and I/O wait times. It's ideal for benchmarking CPU-intensive algorithms and measuring computational performance.
