Python

Python time clock() Method

One of the most effective languages in use today is Python, a high-level programming language. It is a popular programming language that both seasoned developers and new developers use. It is also recommended that developers who want to learn to code should start with Python language instead of any other language. There are many built-in functions in Python that make programming simple and more manageable for every level of programmer. The time clock() function is one of the built-in modules in Python that provides various time-related functions. It is a part of Python’s default library. This library is specifically used for time-related functions. In this article, you will get a quick overview of the time clock() function with some relevant examples to help you learn the implementation.

Time clock() Method

Python provides a series of very important and useful time-related functions. These functions are part of Python’s standard library that contains time-related utilities. The clock() function of the time module is used to get the time of the CPU or the real-time of a process since it has started.

The point to remember is that the clock() function is platform-dependent. Because the clock() function is platform dependent, it will behave differently for each operating system, such as Windows, Linux, macOS, or UNIX-based operating systems. For example, when the clock() function is executed in Microsoft Windows, it will return the current wall clock time in the real world since the program started. However, if it is run on a UNIX-based system, it will return the processing time of the CPU in seconds in the form of a floating point. Now let us explore some implemented examples to understand the function of the time clock() method.

Example 1:

In this example, we are going to use the time.clock() function of the time module to get the current CPU processing time. As discussed above, the clock() function is a platform-dependent function that became the reason for its depletion. It was deprecated in Python version 3.3 and removed in version 3.8. However, let us learn the functioning of the clock() method with the help of a simple and short example.

Refer to the code below to learn about the clock() module. The syntax is time.clock(), it does not take any parameter and returns a current CPU time in case of UNIX and returns a current clock time in case of windows. Now let us get the CPU processing time with time.clock() function.

import time

clockTime = time.clock()

print("The real time CPU processing time is:", clockTime)

Refer to the output below to see what the current processing time is.

As you can see, the time.clock() has returned the current CPU time in seconds and in the form of a floating point.

Example 2:

Now that we have learned how the time.clock() function returns the CPU processing time in seconds with a simple and short example. In this example, we are going to see a long and a bit complex factorial function to see how the processing time gets affected. Let us see the code below, and then we will explain the whole program step by step.

import time
def factorial(x):
    fact = 1
    for a in range(x, 1, -1):
        fact = fact * a      
    return fact
print("CPU time at the beginning: ", time.clock(),"\n\n")
i = 0
f = [0] * 10;
while i < 10:
    f[i] = factorial(i)
    i = i + 1
for i in range(0, len(f)):
    print("The factorial of % d is:" % i, f[i])
print("\n\nCPU time at the end: ", time.clock(),'\n\n')

First, the time module is imported into the program, as it was done in the first example, then a factorial function is defined. The factorial function() takes an argument ‘x’ as input, calculates its factorial, and returns the calculated factorial ‘fact’ as output. The processor time is checked at the beginning of the program execution with the time.clock() function and at the end of the execution as well to see the elapsed time in between the whole process. A ‘while’ loop is used to find the factorial of 10 numbers ranging from 0 to 9. Refer to the output below to see the result:

As you can see, the program started at 87.9081455 seconds and ended at 87.9154967 seconds. Hence, the elapsed time is just 0.0073512 seconds.

Example 3:

As discussed above, the time.clock() function will be removed in Python version 3.8 because it is a platform-dependent function. The question here is what we will do when time.clock() is no longer available. The answer is the most commonly used function of Python which is time.time(). It is given in the time module of Python. It accomplishes the same tasks as the time.clock() function does. The time.time() function in the time module gives the current time in seconds and in the form of a floating point number.

The advantage of the time.time() function over the time.clock() function is that it is a platform-independent function. The result of the time.time() function is not affected if the operating system changes. Now, let us compare the results of both functions with the help of an example and see the usage of both functions. Refer to the code given below to understand the difference in the functioning of time.time() and time.clock() functions.

import time

tc = time.clock()

print ("The result of time.clock() function is:", tc)

tt = time.time()

print("\n\nCPU time at the end: ", time.clock(),'\n\n')

In the code given above, we simply assigned the time.clock() function to a variable (tc in our case) and time.time() to another variable (tt as you can see in the code) and simply get both values printed out. Now consider the output of both the functions:

Text Description automatically generated

As you can see the time.clock() function has returned the current processor time, however, the time.time() function has returned the current wall time in seconds. Both functions have returned the time value in floating point numbers.

Please note that the time.time() is platform independent function, so if you run it on Linux, UNIX, etc, you will get the same result. To ensure that, try running the above code on windows, UNIX, and Linux simultaneously.

Conclusion

The time module of Python was covered in this article, along with a brief overview and some examples. We have primarily discussed the two functions, i.e., time.clock() and time.time(). This article was specifically designed for the time.clock() function. These examples depict the concept and usage of the clock() method in Python.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content