A brief introduction to Python’s Peephole optimization technique
Before we dive into the details of peephole optimization, let us try to understand how Python code is executed. As the Python program runs, source code gets compiled into a set of instructions called
bytecode. This intermediate
bytecode is stored with
.pyc extension under
__pycache__ folder which is then executed by the Python interpreter. This
.pyc files contain a "faster" or "optimized" version of the source code.
Image by Author
The compiled code object is accessible via the attribute
__code__ on the function and carries a few important attributes. Here we are interested in
co_consts. We will understand these when we go thru the examples below.
co_constsreturns a tuple of any literals that occur in the function body
co_varnamesreturns a tuple containing the names of any local variables used in the function body
co_namesreturns a tuple of any non-local names referenced in the function body
🔵 Intellipaat Data Science with Python course: https://intellipaat.com/python-for-data-science-training/In this Data Science With Python Training video, you...
Enroll in our Data Science with Python training in Chennai. Best Data Science with Python Training courses in Chennai for 100% Job Placements Support.
🔥Intellipaat Python for Data Science Course: https://intellipaat.com/python-for-data-science-training/In this python for data science video you will learn e...
Master Applied Data Science with Python and get noticed by the top Hiring Companies with IgmGuru's Data Science with Python Certification Program. Enroll Now
Become a data analysis expert using the R programming language in this [data science](https://360digitmg.com/usa/data-science-using-python-and-r-programming-in-dallas "data science") certification training in Dallas, TX. You will master data...