Skip to content

Python Timer Functions: Three Ways to Monitor Your Code

CodeMDD.io

Python is widely recognized as an effective programming language, but it’s important to note that pure Python programs may run more slowly compared to their counterparts in compiled languages such as C, Rust, and Java. This tutorial aims to teach you how to use a Python timer to monitor the speed at which your programs are running.

Throughout this tutorial, you will learn how to utilize various Python timer functions, including time.perf_counter(), to measure the execution time of your code. Additionally, you’ll gain an understanding of key concepts such as classes, context managers, and decorators, which play a crucial role in many Python programming scenarios.

Python Timers

Before diving into the details of Python timers, let’s take a look at a sample code that will be used throughout this tutorial. Later, we’ll incorporate a Python timer to monitor its performance.

from reader import feed
def main():
tutorial = feed.get_article(0)
print(tutorial)
if __name__ == "__main__":
main()

Python Timer Functions

The Python standard library provides several functions that allow us to measure time. Some of the key timer functions include:

  • monotonic()
  • perf_counter()
  • process_time()
  • time()

Starting from Python 3.7, additional functions such as thread_time() and nanosecond versions of the original functions were introduced, with the latter having an _ns suffix. For instance, perf_counter_ns() is the nanosecond version of perf_counter().

For the purpose of this tutorial, we’ll mainly work with perf_counter(). The perf_counter() function returns the value, in fractional seconds, of a performance counter. It provides the highest resolution for measuring short durations.

Example: Download Tutorials

To demonstrate the different ways of adding a Python timer to your code, we’ll apply different timer functions to the same code example mentioned earlier. However, feel free to apply these techniques to your own code if you have existing scripts that you’d like to measure.

Once installed, import the package as reader and save the example code in a file named latest_tutorial.py.

from reader import feed
def main():
tutorial = feed.get_article(0)
print(tutorial)
if __name__ == "__main__":
main()

Your First Python Timer

Now, let’s add a basic Python timer to the example code using the time.perf_counter() function. This function serves as a reliable performance counter for timing specific parts of your code.

Keep in mind that perf_counter() measures time in seconds from an unspecified moment, making the return value of a single function call irrelevant. However, by calculating the difference between two calls to perf_counter(), you can accurately determine the elapsed time between those calls.

Here’s an example showcasing the usage of perf_counter():

import time
start_time = time.perf_counter()
# Code to be timed
end_time = time.perf_counter()
execution_time = end_time - start_time
print(f"Execution time: {execution_time} seconds")

Incorporating this concept into our example, we can now implement a Python timer as follows:

import time
from reader import feed
def main():
start_time = time.perf_counter()
tutorial = feed.get_article(0)
end_time = time.perf_counter()
execution_time = end_time - start_time
print(tutorial)
print(f"Execution time: {execution_time} seconds")
if __name__ == "__main__":
main()

By adding the timer, we can now monitor the performance of our code. When running the script, the output will display the latest tutorial as well as the execution time of the code in seconds.

Conclusion

In conclusion, this tutorial has introduced you to three different ways of monitoring your code using Python timers. By incorporating timer functions such as time.perf_counter(), you can accurately measure the execution time of your programs. Additionally, you’ve gained insights into useful concepts like classes, context managers, and decorators, which can enhance your Python programming skills further.

Each timer function discussed in this tutorial serves a specific purpose, and depending on your requirements, you now have the knowledge to choose the most suitable option. Furthermore, with a working Python timer at your disposal, you can monitor and optimize the performance of your programs.

Remember, Python timers are powerful tools that allow you to gain valuable insights into the speed of your code execution. By effectively utilizing timers, you can identify bottlenecks and optimize the performance of your Python scripts.

CodeMDD.io