Python time clock() Method



The Python time clock() method is used to get the current processor time. It is returned as a floating point number expressed in seconds. If the amount of time required to execute a program is to be calculated, the method needs to be invoked subsequently. The difference between them is considered as the time taken for the program to be executed.

This method is platform dependent; the current processor time is expressed in seconds on Unix. The precision depends on that of the C function of the same name, but in any case, this is the function to use for benchmarking Python or timing algorithms.

On Windows, this function returns wall-clock seconds elapsed since the first call to this function as a floating point number, based on the Win32 function QueryPerformanceCounter.

Note: Not all systems can measure the true process time. On such systems (including Windows), clock usually measures the wall time since the program was started. The program is deprecated in Python versions after 3.3.

Syntax

Following is the syntax for the Python time clock() method −

time.clock()

Parameters

The method does not accept any parameters.

Return Value

This method returns the current processor time as a floating point number expressed in seconds.

Example

The following example shows the usage of the Python time clock() method. We are simply retrieving the process time using this method. This example is only executable in Python 2.

import time

tm = time.clock()

print "Process time:", tm

When we run above program, it produces following result −

Process time: 0.016222

Example

The Python time clock() method gets the process time; and as might already know, process time differs from the wall time. They are both not to be confused.

In this example, we are comparing the return values of this method and time() method. This method returns the process time whereas the time() method returns wall time. This example is only executable in Python 2.

import time

def procedure():
   time.sleep(2.5)

# measure process time
t0 = time.clock()
procedure()
print time.clock(), "seconds process time"

# measure wall time
t0 = time.time()
procedure()
print time.time() - t0, "seconds wall time"

Let us compare

0.0 seconds process time
2.50023603439 seconds wall time

Example

If we want to retrieve the amount of time taken to execute a program, the clock() method needs to be called subsequently.

In the following example, we are calling this method at the start of the program and the end of the program, the difference between both these time stamps will be the amount of time taken for the program to be executed. Again, this is only executable in Python 2.

import time

# Record the start process time
start = time.clock()
print "Starting process time:", start

# Performing addition task
i = 20
a = i + 30
print "Task:", a

# Ending process time
end = time.clock()
print "Ending process time:", end

print "Total amount of time taken:", (start-end)

The output for the program above is −

Starting process time: 0.015982
Task: 50
Ending process time: 0.016011
Total amount of time taken: -2.9e-05
python_date_time.htm
Advertisements