Python3 time clock() Method
Description
The time.perfcounter() or time.processtime() method is recommended as a replacement.
The Python time clock() function returns the current CPU time as a floating-point number of seconds. This is useful for measuring the time taken by different programs, providing more accurate results than time.time().
It's important to note that the meaning of this function varies across different systems. On UNIX systems, it returns the "process time," which is a floating-point number of seconds (timestamp). On Windows, the first call returns the actual time taken by the process. Subsequent calls return the elapsed time since the first call. (This is based on the WIN32 QueryPerformanceCounter(), which is more precise than milliseconds.)
Syntax
The syntax for the clock() method is:
time.clock()
Parameters
- NA.
Return Value
This function has two functionalities:
- On the first call, it returns the actual time taken by the program.
- On subsequent calls, it returns the elapsed time since the first call.
On win32 systems, this function returns the real time (wall time), while on Unix/Linux systems, it returns the CPU time.
Example
The following example demonstrates the usage of the clock() function:
#!/usr/bin/python3
import time
def procedure():
time.sleep(2.5)
# time.clock
t0 = time.clock()
procedure()
print(time.clock() - t0)
# time.time
t0 = time.time()
procedure()
print(time.time() - t0)
The output of the above example is:
5.000000000000143e-05
2.5020556449890137