Which one is more accurate in between time.clock() vs. time.time()?

Two commonly used functions from the Python time module are time.time() and time.clock(). Each function provides a different purpose and returns different values depending on the platform (Windows vs. Unix).

In Python 3.8, time.clock() was removed, so time.perf_counter() or time.process_time() are generally preferred over the older time.clock() for specific CPU time measurements.

The time.clock() was designed for measuring process CPU time, while time.time() measures wall-clock time. time.time() is more accurate for measuring overall elapsed time (the duration of time that has passed between two specific points in time).

Measuring Elapsed Time with time.time()

The time.time() function returns the number of seconds since the epoch (January 1, 1970, 00:00:00 UTC) as a floating point number. It's generally used to measure elapsed wall-clock time ?

import time

start = time.time()
# Simulate some processing task
for _ in range(1000000):
    pass
end = time.time()

print(f"Elapsed time using time.time(): {end - start} seconds")

The output of the above code is ?

Elapsed time using time.time(): 0.028861045837402344 seconds

Measuring Elapsed Time with time.clock() (Deprecated)

Now, let's measure the same using time.clock() function. Note that in Python 3.3, time.clock() is deprecated and replaced by time.perf_counter() for high-resolution timing across platforms ?

import time

try:
    start = time.clock()
    for i in range(1000000):
        pass
    end = time.clock()
    print("CPU time:", end - start, "seconds")
except AttributeError:
    print("time.clock() is not available in this Python version")

The output of the above code is ?

time.clock() is not available in this Python version

Using time.process_time() Function

The time.process_time() function is a replacement for time.clock() for measuring CPU time. It returns the CPU time used by the process and is consistent across platforms ?

import time

start = time.process_time()
for i in range(1000000):
    pass
end = time.process_time()

print(f"CPU time used: {end - start} seconds")

The output of the above code is ?

CPU time used: 0.025048299999999986 seconds

Using time.perf_counter() for High-Resolution Timing

For the most accurate timing measurements, Python 3.3+ provides time.perf_counter(), which offers the highest available resolution and is monotonic ?

import time

start = time.perf_counter()
for i in range(1000000):
    pass
end = time.perf_counter()

print(f"High-resolution time: {end - start} seconds")

The output of the above code is ?

High-resolution time: 0.024567800000001234 seconds

Comparison of Timing Functions

Function Purpose Availability Best For
time.time() Wall-clock time All Python versions General elapsed time
time.clock() CPU time (deprecated) Removed in Python 3.8 Legacy code only
time.process_time() CPU time Python 3.3+ CPU usage measurement
time.perf_counter() High-resolution timing Python 3.3+ Precise benchmarking

Conclusion

For modern Python applications, use time.perf_counter() for high-precision timing and time.process_time() for CPU time measurements. time.time() remains useful for wall-clock time, while time.clock() should be avoided as it's deprecated.

Updated on: 2026-03-24T19:20:44+05:30

740 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements