Time clock() Method in Python

1. What is the time.clock() Method in Python?

The clock() method was a function in the time module that measured processor time. It was useful for benchmarking code execution but had platform-dependent behavior:

  • On Windows, it measured wall-clock time since the first call.
  • On Linux/macOS, it measured CPU time.

However, Python deprecated time.clock() in version 3.3 and removed it in Python 3.8 due to its inconsistent behavior.

2. Syntax of time.clock()

import time
time.clock()

This function returned a floating-point number representing time in seconds.

3. Example of time.clock() (Before Python 3.8)

import time

start = time.clock()  # Start measuring time

# Simulating a task
for i in range(1000000):
    pass

end = time.clock()  # End measuring time

print("Execution Time:", end - start)

Expected Output (Before Python 3.8):

Execution Time: 0.056789  # (example value, varies per system)

4. Why Was time.clock() Removed?

  1. Platform Inconsistency: It behaved differently on Windows vs. Unix-based systems.
  2. Better Alternatives Available: Python introduced time.perf_counter() and time.process_time(), which are more reliable.
  3. Deprecation & Removal:
    • Python 3.3: Marked as deprecated.
    • Python 3.8: Completely removed.

If you try to use time.clock() in Python 3.8 or later, you will get this error:

AttributeError: module 'time' has no attribute 'clock'

5. Alternative Methods for Measuring Time

Since clock() is no longer available, you should use these modern alternatives:

(i) time.perf_counter() – Best for Performance Benchmarking

  • Measures wall-clock time (real elapsed time).
  • High accuracy.
  • Works consistently across all platforms.

Example:

import time

start = time.perf_counter()  # Start measuring time

# Simulating a task
for i in range(1000000):
    pass

end = time.perf_counter()  # End measuring time

print("Execution Time:", end - start)

Expected Output:

Execution Time: 0.045632  # (example value, varies per system)

(ii) time.process_time() – Best for Measuring CPU Usage

  • Measures only CPU execution time.
  • Ignores sleep and system delays.
  • Useful for profiling CPU-intensive operations.

Example:

import time

start = time.process_time()  # Start measuring CPU time

# Simulating a task
for i in range(1000000):
    pass

end = time.process_time()  # End measuring CPU time

print("CPU Time:", end - start)

Expected Output:

CPU Time: 0.032145  # (example value, varies per system)

6. Key Differences Between perf_counter() and process_time()

MethodMeasuresIncludes Sleep Time?High Precision?
time.perf_counter()Elapsed time (wall-clock)YesYes
time.process_time()CPU time (execution only)NoYes

7. Which One Should You Use?

  • Use time.perf_counter() for measuring elapsed time (real-world performance).
  • Use time.process_time() for measuring CPU time (ignores sleep and waiting).

8. Conclusion

  • The time.clock() method is no longer available in Python 3.8 and later.
  • Use time.perf_counter() for accurate elapsed time measurement.
  • Use time.process_time() to measure CPU execution time.