While many developers recognize Python as an effective programming language, pure Python programs may run more slowly than their counterparts in compiled languages like C, Rust, and Java. In this tutorial, you’ll learn how to use a Python timer to monitor how quickly your programs are running.
In this tutorial, you’ll learn how to use:
time.perf_counter() to measure time in Python
Classes to keep state
Context managers to work with a block of code
Decorators to customize a function
You’ll also gain background knowledge into how classes, context managers, and decorators work. As you explore examples of each concept, you’ll be inspired to use one or several of them in your code, for timing code execution, as well as in other applications. Each method has its advantages, and you’ll learn which to use depending on the situation. Plus, you’ll have a working Python timer that you can use to monitor your programs!
Decorators Q&A Transcript: Click here to get access to a 25-page chat log from our Python decorators Q&A session in the Real Python Community Slack where we discussed common decorator questions.
Python Timers
First, you’ll take a look at some example code that you’ll use throughout the tutorial. Later, you’ll add a Python timer to this code to monitor its performance. You’ll also learn some of the simplest ways to measure the running time of this example.
Python Timer Functions
If you check out the built-in time module in Python, then you’ll notice several functions that can measure time:
monotonic()
perf_counter()
process_time()
time()
Python 3.7 introduced several new functions, like thread_time(), as well as nanosecond versions of all the functions above, named with an _ns suffix. For example, perf_counter_ns() is the nanosecond version of perf_counter(). You’ll learn more about these functions later. For now, note what the documentation has to say about perf_counter():
Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. (Source)
First, you’ll use perf_counter() to create a Python timer. Later, you’ll compare this with other Python timer functions and learn why perf_counter() is usually the best choice.
Example: Download Tutorials
To better compare the different ways that you can add a Python timer to your code, you’ll apply different Python timer functions to the same code example throughout this tutorial. If you already have code that you’d like to measure, then feel free to follow the examples with that instead.
The example that you’ll use in this tutorial is a short function that uses the realpython-reader package to download the latest tutorials available here on Real Python. To learn more about the Real Python Reader and how it works, check out How to Publish an Open-Source Python Package to PyPI. You can install realpython-reader on your system with pip:
Then, you can import the package as reader.
You’ll store the example in a file named latest_tutorial.py. The code consists of one function that downloads and prints the latest tutorial from Real Python:
2
3from reader import feed
4
5def main():
6 “””Download and print the latest tutorial from Real Python”””
7 tutorial = feed.get_article(0)
8 print(tutorial)
9
10if __name__ == “__main__”:
11 main()
realpython-reader handles most of the hard work:
Line 3 imports feed from realpython-reader. This module contains functionality for downloading tutorials from the Real Python feed.
Line 7 downloads the latest tutorial from Real Python. The number 0 is an offset, where 0 means the most recent tutorial, 1 is the previous tutorial, and so on.
Line 8 prints the tutorial to the console.
Line 11 calls main() when you run the script.
When you run this example, your output will typically look something like this:
# Python Timer Functions: Three Ways to Monitor Your Code
While many developers recognize Python as an effective programming language,
pure Python programs may run more slowly than their counterparts in compiled
languages like C, Rust, and Java. In this tutorial, you’ll learn how to use
a Python timer to monitor how quickly your programs are running.
[ … ]
## Read the full article at https://realpython.com/python-timer/ »
* * *
The code may take a little while to run depending on your network, so you might want to use a Python timer to monitor the performance of the script.
Read the full article at https://realpython.com/python-timer/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]