Welcome Python 3.11, an Upgrade to Counter Decades-Long Speed Issue

Welcome Python 3.11, an Upgrade to Counter Decades-Long Speed Issue

Python 3.11 is up to 10-60% faster than Python 3.10. On average, developers measured a 1.25x speedup on the standard benchmark suite.

Python is incredibly popular because it's easy to learn, versatile, and has thousands of useful libraries for data science. But one thing, it is not as fast. That's about to change in Python 3.11, currently in the first beta phase of its preview (version 3.11.0b1) ahead of its stable release later this year. It is one of the most keenly anticipated versions. "Python 3.11 is up to 10-60 percent faster than Python 3.10," state the release notes.

How is this being done? Python 3.11 is the first release to benefit from a project called Faster CPython, where CPython is the standard version of the interpreter. Faster CPython is a project funded by Microsoft, whose members include Python inventor Guido van Rossum, Microsoft senior software engineer Eric Snow, and Mark Shannon – who is under contract to Microsoft as tech lead for the project.

A session scheduled for the EuroPython event to be held in Dublin in July centers on some of the changes that enable the speed-up. Shannon will describe the "adaptive specializing interpreter" in Python 3.11, which is PEP (Python Enhancement Proposal) 659. This describes a technique called specialization which, Shannon explains, "is typically done in the context of a JIT [just in time] compiler, but research shows specialization in an interpreter can boost performance significantly."

The interpreter identifies code that can benefit from specialization and "once an instruction in a code object has executed enough times, that instruction will be "specialized" by replacing it with a new instruction that is expected to execute faster for that operation," states the PEP. The speed up can be "up to 50 percent."

New Features
  • Enhanced error locations in tracebacks
  • When printing tracebacks, the interpreter will now point to the exact expression that caused the error instead of just the line.
 For example:

Traceback (most recent call last):

  File "distance.py", line 11, in

    print(manhattan_distance(p1, p2))

          ^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "distance.py", line 6, in manhattan_distance

    return abs(point_1.x – point_2.x) + abs(point_1.y – point_2.y)

                           ^^^^^^^^^

AttributeError: 'NoneType' object has no attribute 'x'

Previous versions of the interpreter would point to just the line making it ambiguous which object was None. These enhanced errors can also be helpful when dealing with deeply nested dictionary objects and multiple function calls,

Traceback (most recent call last):

  File "query.py", line 37, in

    magic_arithmetic('foo')

    ^^^^^^^^^^^^^^^^^^^^^^^

  File "query.py", line 18, in magic_arithmetic

    return add_counts(x) / 25

           ^^^^^^^^^^^^^

  File "query.py", line 24, in add_counts

    return 25 + query_user(user1) + query_user(user2)

                ^^^^^^^^^^^^^^^^^

  File "query.py", line 32, in query_user

    return 1 + query_count(db, response['a']['b']['c']['user'], retry=True)

                               ~~~~~~~~~~~~~~~~~~^^^^^

  • TypeError: 'NoneType' object is not subscriptable as well as complex arithmetic expressions:

Traceback (most recent call last):

  File "calculation.py", line 54, in

    result = (x / y / z) * (a / b / c)

              ~~~~~~^~~

ZeroDivisionError: division by zero

  • New syntax features:

                     PEP 654: Exception Groups and except*. (Contributed by Irit Katriel in bpo-45292.)

New typing features:

PEP 646: Variadic generics.

PEP 655: Marking individual TypedDict items as required or potentially missing.

PEP 673: Self type.

PEP 675: Arbitrary literal string type.

Security improvements:

New -P command-line option and PYTHONSAFEPATH environment variable to not prepend a potentially unsafe path to sys.path such as the current directory, the script's directory or an empty string.

Other Python 3.11 additions, fixes, and changes

Many smaller improvements also landed in Python 3.11:

  • Python objects require less memory, as their namespaces are now lazily created, and their namespace dictionaries now share keys whenever possible
  • Dictionaries where all keys are Unicode no longer need to store hashes, thus reducing the size of the dictionary and allowing more cache efficiency
  • The CPython runtime, the reference interpreter for Python, now has experimental support for being compiled to WebAssembly. This may aid the future development of projects like PyScript, which allow a WASM-compiled Python runtime to operate in the browser

Disclaimer: Analytics Insight does not provide financial advice or guidance. Also note that the cryptocurrencies mentioned/listed on the website could potentially be scams, i.e. designed to induce you to invest financial resources that may be lost forever and not be recoverable once investments are made. You are responsible for conducting your own research (DYOR) before making any investments. Read more here.

Related Stories

No stories found.
logo
Analytics Insight
www.analyticsinsight.net