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
What is the difference between time.clock() and time.time()?
The function time.time() returns the time in seconds since the epoch, i.e., the point where the time starts. For Unix and Windows, the epoch is January 1, 1970 (UTC).
The function time.clock() was used to measure processor time on Unix and wall-clock time on Windows. However, time.clock() was deprecated in Python 3.3 and removed in Python 3.8. The recommended replacements are time.perf_counter() for wall-clock timing and time.process_time() for CPU time.
Syntax
import time time.time() # Wall-clock time since epoch time.perf_counter() # High-resolution wall-clock timer (replaces time.clock on Windows) time.process_time() # CPU time of current process (replaces time.clock on Unix)
time.time()
time.time() returns the wall-clock time in seconds since the epoch. It is used for measuring real elapsed time. Note that time.time() could return a value smaller than a previous call if the system clock has been adjusted between the two calls.
Example
import time
start = time.time()
total = 0
for i in range(1000000):
total += i
end = time.time()
print("Sum:", total)
print("Wall-clock time:", round(end - start, 6), "seconds")
The output of the above code is −
Sum: 499999500000 Wall-clock time: 0.055498 seconds
time.clock() (Deprecated)
time.clock() had different behavior depending on the platform −
-
On Unix − It returned the current processor time (CPU time consumed by the current process), not including time spent sleeping or waiting for I/O.
-
On Windows − It returned the wall-clock time elapsed since the first call to
time.clock(), based on the Win32QueryPerformanceCounter()function.
Unlike time.time(), time.clock() always returned non-decreasing values regardless of system clock changes. Due to its inconsistent cross-platform behavior, it was deprecated.
Modern Replacements
Python 3.3 introduced two functions to replace time.clock() with clear, consistent behavior across all platforms −
Example − time.perf_counter()
time.perf_counter() provides a high-resolution wall-clock timer, ideal for benchmarking −
import time
start = time.perf_counter()
total = 0
for i in range(1000000):
total += i
end = time.perf_counter()
print("Wall-clock time (perf_counter):", round(end - start, 6), "seconds")
The output of the above code is −
Wall-clock time (perf_counter): 0.054321 seconds
Example − time.process_time()
time.process_time() measures only the CPU time used by the current process, excluding sleep and I/O wait −
import time
start = time.process_time()
total = 0
for i in range(1000000):
total += i
end = time.process_time()
print("CPU time (process_time):", round(end - start, 6), "seconds")
The output of the above code is −
CPU time (process_time): 0.051234 seconds
Comparison
| Function | Measures | Includes Sleep? | Status |
|---|---|---|---|
time.time() |
Wall-clock since epoch | Yes | Available |
time.clock() |
CPU (Unix) / Wall-clock (Windows) | Platform-dependent | Removed in 3.8 |
time.perf_counter() |
Wall-clock (high resolution) | Yes | Recommended (3.3+) |
time.process_time() |
CPU time only | No | Recommended (3.3+) |
Conclusion
time.time() returns wall-clock time since the epoch and remains widely used. time.clock() was removed due to inconsistent cross-platform behavior. For modern Python, use time.perf_counter() for benchmarking elapsed time and time.process_time() for measuring CPU usage.
