Python time clock() method
Description
The clock() method was removed in Python 3.8; you can use the time.perf_counter() or time.process_time() method instead.
The Python time clock() function returns the current CPU time in seconds as a floating-point number. It is used to measure the elapsed time of different programs and is more useful than time.time() for this purpose.
Note that its meaning differs between systems. On UNIX it returns "processor time" as a floating-point number of seconds (a timestamp). On WINDOWS, the first call returns the actual elapsed time of the process, and subsequent calls return the elapsed time since the first call (in fact it is based on WIN32's QueryPerformanceCounter(), which is more precise than millisecond resolution).
Syntax
clock() method syntax:
time.clock()
Parameters
- NA.
Return Value
This function has two behaviors:
On the first call, it returns the actual elapsed time of the program;
On the second and later calls, it returns the interval from the first call to the current call
On win32 systems this function returns wall time, while on Unix/Linux it returns CPU time.
Example
The following example shows how to use the clock() function:
Example
#!/usr/bin/python
import
time
def
procedure
(
)
:
time
.
sleep
(
2.5
)
measure process time
t0
=
time
.
clock
(
)
procedure
(
)
time
.
clock
(
)
- t0
,
"seconds process time"
measure wall time
t0
=
time
.
time
(
)
procedure
(
)
time
.
time
(
)
- t0
,
"seconds wall time"
The output of the above example is:
3.3e-05 seconds process time
2.50329995155 seconds wall time