How To Run Code Every Minute Exactly Python?
Although many developers agree Python is a good programming language, pure Python projects could run slower than those in compiled languages as Java, C, and Rust. This course will teach you how to track program execution speed using a Python timer.
You will also get background understanding of how decorators, context managers, and classes operate. Examining samples of each idea will motivate you to apply one or more of them in your code for timing code execution as well as in other applications. Every approach has benefits; you will learn which to apply based on the circumstance. You can also track your scripts using the functioning Python timer that comes included!
Python clocks
You will first review some sample code you will need all through the lesson. You will then include a Python timer into this code to track its performance. Additionally, you will pick up some of the easiest techniques for timing the running of this example.
Timer Functions in Python
Examining Python’s built-in time module can help you to find numerous time measuring capabilities:
monotonic()
perf_counter()
process_time()
time()
Along with nanosecond versions of all the aforementioned, denoted with a _ns suffix, Python 3.7 added numerous fresh features including thread_time(). Perf_counter_ns() for instance is the nanosecond variant of perf_counter(). Later on you will learn more about their purposes. Note just now what the documentation says about perf_counter():
You first will establish a Python timer with perf_counter(). You will later contrast this with other Python timer methods and see why perf_counter() is often the best option.
For instance, get tutorials.
Throughout this lesson, you will use many Python timer methods to the same code sample in order to better evaluate the several ways you may include a Python timer into your code. If you already have code you wish to measure, then feel free to follow the samples with that instead.
The brief method you will be using in this tutorial downloads the most recent tutorials accessible here on Real Python using the realpython-reader package. See How to Publish an Open-Source Python Package to PyPI to learn further about the Real Python Reader and how it operates. pip will let you install realpython-reader on your machine:
$ python -m pip install realpython-reader
You will keep the example in a file called latest_tutorial.py. The code runs one function downloading and printing the most recent Real Python tutorial:
# latest_tutorial.py
from reader import feed
def main():
"""Download and print the latest tutorial from Real Python"""
tutorial = feed.get_article(0)
print(tutorial)
if __name__ == "__main__":
main()
Most of the effort is handled by realpython-reader.
Line 3 imports from Realpython-Reader. Tutorial downloading from the Real Python feed is covered in this module.
Line 7 downloads Real Python’s newest lesson. Zero is an offset; 0 denotes the most current tutorial; 1 denotes the preceding tutorial; so on.
Line 8 runs the console’s instructional.
Line 11 invokes main() upon script running.
Running this example usually results in your output looking somewhat like:
$ python latest_tutorial.py
# 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/ »
* * *
Depending on your network, the code could execute a bit slowly; so, you could wish to keep an eye on script performance using a Python timer.
Your First Python Timer
Now you will add to the example using time.perf_counter a basic Python timer. Once more, this is a performance counter fit for timing portions of your code.
The return value of one call to the procedure is not helpful as perf_counter() counts the seconds from some unknown period in time. But you can determine the number of seconds elapsed between two calls to perf_counter by examining their differences: