Python3 time clock() Method


Description

Python 3.8 has removed the clock() methodYou can usetime.perf_counter()ortime.process_time()as a replacement method.

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().

Note that its meaning differs on different systems. On UNIX systems, it returns "process time", which is a floating-point number expressed in seconds (timestamp). On WINDOWS, the first call returns the actual time the process has been running. Calls after the second time return the running time from the first call to the present. (It is actually based on QueryPerformanceCounter() on WIN32, which is more precise than millisecond representation.)

Syntax

Syntax of the clock() method:

time.clock()

Parameters

  • NA。

Return Value

This function has two functions:

On the first call, it returns the actual time the program has been running;

On calls after the second, it returns the time interval from the first call to this call.

Under the win32 system, this function returns the real time (wall time), while under Unix/Linux it returns CPU time.

Examples

The following example demonstrates how to use 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
Other Extensions