
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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. The 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.
Example
The following program illustrates the usage of time.time() function. By running this program, we can see the total seconds taken for the loop.
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")
Following is the output of the above code -
Elapsed time using time.time(): 0.028861045837402344 seconds
Measuring Elapsed Time with time.clock() (Deprecated)
Now, let's measure the same by 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.
Example
The program attempts to measure CPU time using time.clock(), but this function is deprecated and unavailable, causing an AttributeError. It iterates a million times without doing anything.
import time start = time.clock() for i in range(1000000): pass end = time.clock() print("CPU time:", end - start, "seconds")
Following is the output of the above code -
Traceback (most recent call last): File "/home/main.py", line 3, in <module> start = time.clock() ^^^^^^^^^^ AttributeError: module 'time' has no attribute 'clock'
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.
Example
The following example demonstrates how time.process_time() function measures the CPU time. Unlike time.time(), this method does not include time elapsed during sleep or I/O operations.
import time start = time.process_time() for i in range(1000000): pass end = time.process_time() print("CPU time used:", end - start, "seconds")
Following is the output of the above code -
CPU time used: 0.025048299999999986 seconds