Python Optimization

  • By Mayuresh Marale
  • March 31, 2023
  • Python
Python Optimizations

Python Optimization

Highlights

Python Optimization is as follows:

  1. Caching Small Integers
  2. String Interning
  3. Constant Folding
  4. Dead Code Elimination

 

Introduction to Python Optimization

Python is the most popular programming language. Python creates the same impact with less code. But it scores very poorly on performance factors. To tackle this poor performance. The Python official team has optimized cpython implementation over all these years, making Python more efficient while also preserving its impact.

 

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

 

Various Python Optimizations are as follows:

 

  • Caching Small Integers

 

Python caches small integers in the internal array from -5 to 256 after initialization. Whenever a new name is assigned with one of such cached integer values, python does not create a brand new object for cached value and just returns a reference to a pre-existing object.

Enroll in SevenMentor’s Best Python Classes in Pune today and take the first step towards a rewarding career in programming!

Python Optimizations

 

As expressed in the above example, names a and b have similar values in each example, hence equality test (a == b) for all above examples is True, however identity test (a is b) is True only for the range of integer values -5 to 256, but False for values upto -6 and 257 onwards. Here, With -6, we have a different outcome, because a and b point to different objects, even though they have the very same value.

 

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

 

Python Optimizations

 

Python caches small numbers that are very often used in arithmetic operations, as boundaries in loops and as results of small computations.

  • String Interning

String interning in Python is the mechanism of storing only one copy of each distinct string value in memory.

String interning has two advantages:

  1. Save the space in the memory
  2. Save time when comparing strings whose values are the same
  • Implicit interning

Python implicitly interns the strings that have _ and alphanumeric ASCII character values.

Until Python v3.7, the peephole optimization is used for string interning and all strings longer than 20 characters will not be interned. However, in Python v3.8 onwards, the peephole was changed to AST optimizer, and interning is supported up to length 4096 rather than 20.

  • Explicit interning

Strings that do not follow implicit interning rules and /or strings with length 4097 onwards are not interned implicitly. Hence we can intern such strings using sys.intern module.

 

Python Optimizations

 

  • Constant Folding

Constant folding is a mechanism utilized by compilers and interpreters for precomputing in compile time the expressions that have no runtime dependencies. 

Refer for example as follow:

 

Python Optimizations

 

Constant folding is performed by the interpreter for various expressions for objects like int, str, and tuples.

Python interpreter folds that expression into a constant while compiling the source code (.py) into bytecode (.pyc). It means that the resulting bytecode won’t contain the 100 * 100 expression, but the 10000 constant instead. Likewise in the case of strings and tuples, expressions are precomputed and folded into constant outcomes.

We can verify the constant folding mechanism through the dis module checkout bytecode representation of defined Python functions this( ) and that( ), also outcomes of expressions mentioned inside those functions being converted into constants.

  • Dead Code Elimination

 

Python interpreter eliminates dead code, such as unreachable statements, hence no bytecode is generated for such unreachable code line/chunk.

Python Optimizations

 

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

 

Conclusion

 

  • Python optimizations are powerful mechanisms to make the language more space and time efficient.
  • Optimizations are not part of language specifications, and interpreter implementations may contain many such optimizations, as long as the semantics remains the same. Enroll in our Python Course in Pune and get ready to jumpstart your career in programming or data science.
  • Also, the correctness and accuracy of code should never rely on optimizations, as they are a part of release implementations and could be dropped or modified at any time.

For More Information do visit: The Demand For Python Programming

Author:-

Mayuresh Marale
Call the Trainer and Book your free demo Class For Python

Call now!!! | SevenMentor Pvt Ltd.

© Copyright 2021 | SevenMentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*