Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 time clock() Method

Python3 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

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:


#!/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