Timer Python

Programming languages provide many time-related functions. Just like other programming languages, Python provides us with the “time” module to get the current date and time, the time and date of another timestamp, the total time used for a single process, and find out many more things using it. This article will be used to explain some functions of the time module to get the total time taken by a function and also the making of a timer or stopwatch in Python.

Example 01: Using time() Function

Before using any function in our first example, we have to import python’s time module into the code. Without the “time” module, we will not be able to attain our required results. The “import” keyword will be used for this purpose, followed by the module name “time.” We have initialized the first variable, “t1,” that has been taking the current time in seconds from the “time()” method of the time module. Now, here comes the sequence of 5 display print statements along with the sleep() function of time.

Each “print” clause will be used to display some string on the terminal elaborating the current situation in the code or random messages. We have used a total of 4 sleep() functions to make our system sleep for 5 seconds after each print statement execution. After the first 5 print statements, we have initialized another variable, “t2,” that will take the current time after the execution of the previous code. A resultant variable “t” will calculate a difference between the time t2 and t1. This difference will be our total time used in the whole process, and the print clause will display the total time “t.”

Pyhton3 command, along with the name of a python file, will be castoff on the shell to execute the python code. On running, the first display message will be displayed, and the system will sleep for 5 seconds.

After sleeping for the first 5 seconds, a second display message will be shown, and the system will sleep for another 5 seconds.

While the sleep is completed, a second display message will be displayed. Then, our system will sleep for the next 5 seconds.

After the 5 seconds sleep, the next statement will be presented, and a 5-second sleep will be given.

After the 5 seconds of sleep, the last statements will be displayed along with the total time it took to complete the processing.

Example 02: Use of perf_counter() Function

Here comes the perf_counter() function of the time module to calculate the total time taken by a program to get complete. As the name suggests, this function is specifically utilized as the performance counter. In other words, it will let you know the total time a process has taken to complete itself more accurately hence showing the process performance. At the start of this code, we have Imported the time module first and used the “perf_counter()” function to get the current time in the variable “t1”.

We have been using the “for” loop for python, starting from 1 and ending at 5. This “for” loop has been used to print the iteration number and some string text. After the printing of the iteration number along with the text, we have been making our system sleep for the next 2 seconds by casting off the “sleep” function of the time module.

After the “for” loop ends, we will be using the “perf_counter” function once again to get the current time for a particular moment within the new variable “t2”. After this, the resultant time variable “t” will find out the difference between the time t2 and time t1. The last print statement of python is cast-off to show the time difference via the variable “t.”

On execution of this python code, we have got the 5 string values along with their iteration number displayed. After that, the total time this process has taken is also displayed in seconds.

Example 03: Use of process_time() Function

When a user wants to display the total time a code took to complete in fractional seconds, he/she tends to use the “process_time()” function of the time module. We will also elaborate on the use of the process_time() function in this example. The overall code will be quite similar to the above example code. There will be only a change in the replacement of the perf_counter() function with the “process_time()” function. Overall, there is no code change. Save this code.

After executing the updated code, the same 5 string values along with their iteration number are displayed. The total time taken by the “for” loop has been different as the time is calculated in fractional seconds.

Example 04: Use of monotonic() Function

The monotonic function is specially designed to make its time references take on the changes made by a user while the code is executing, i.e., run time. It is necessary because the changes made by any user while run time can cause many variances to the output. We have used the same code as in the above example, replacing the last function with “monotonic().”

The result is the same, but the total time we have got is a little different.

Example 05: Use of While Loop, Divmod Function

If you are looking for an example to create a stopwatch timer for your code, then this example will be a bonus for you. You have to import the time module first in the code. This program will start by taking input from the user via the “input” function at the second last line of the code. The user will input the number or time, and it will be saved to the variable “t.” The count() function will be called by passing the value “t” as input. Within this function, we have been using the “while” loop to continue performing while the time “t” doesn’t reach. Until then, the divmod() function will take “t” to calculate the minutes and seconds if we are adding a large value for time. The format used for the stopwatch has been defined using the “format” function on minutes and seconds and saved to the “timer” variable. The print statement will show the time in the same m:s format on the shell screen. It will be continued until the “t” reaches “-1”.

On execution, a user has entered 20 as a value to the “t” variable. The program will consider it 20 seconds and start the count down in decreasing format.

After a total of 20 seconds, the timer will be ended, and the success string will be displayed.


This is all about the use of the time module in the python program to calculate the total time consumed by the process and create a timer or stopwatch in our examples. You can use this timer stopwatch to stop your code after a specified time. The other time() functions are used to measure the system performance and the code performance as well.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.