hi there serdar here for infoworld in this episode of smart python we're going to look at some
examples of the under the hood speed improvements that have been made in python 3.11. one of the
biggest new changes in 3.11 is the introduction of a new mechanism in the python interpreter
called the specializing adaptive interpreter as defined in python enhancement proposal 659
whenever python encounters a situation where the types of a given operation are predictable
it will use byte codes that are optimized for those specific types and this can yield up to a 25
speed up in your code automatically you don't have to do anything to your code all you have to do
is run it in python 3.11. here's a quick example of the adaptive interpreter work
in this code we have a loop that performs a numerical operation all the types in this loop
are stable and predictable we never work with anything but integers if we run this code with
python 3.10 it it completes in a few seconds run it with python 3.11 though and you'll notice it's
consistently a little bit faster about 15 to 20 percent and that's with no work on our part
this is entirely the interpreter handling this another example of the adaptive interpreter at
work speeding things up is when we have calls to common built-in functions implemented in c
such as the len function which gets the length of an object the runtime will swap in byte codes
that makes calling to those c functions faster so they sidestep the normal calling mechanisms
in this example here i have a simple function call that just returns the length of an array and
when we run this with python 3.10 it runs fairly quickly already but with 3.11 it runs even faster
and again with no work on our part all we've done is swap in the new interpreter
another separate improvement is the way python uses less stack space
and fewer execution frames for each function call for greater efficiency frames are objects
that hold information about program state during a function call and with 3.11 they're only created
when they need to be they used to be created with every single call whether or not they were used
most function calls now don't even use any stack space in the runtime either
so they're more memory efficient and they're more cpu efficient one concrete way you can
measure this improvement is with the performance of a recursive function python doesn't optimize
recursive function calls the way other languages do but 3.11 makes recursion
lots more performant than it used to be here's a common demonstration for recursion this is our old
friend the fibonacci function which calls itself repeatedly when we run this with python 3.10
it takes upwards of 12 seconds to complete its test run of many thousands of operations
and much of that slowness is of course because pure python is not well suited to numerical work
but under python 3.11 the same code takes about 20 percent less time to finish
executing a significant amount of the overhead has just been shaved right off and these may
well just be the first steps that could be made towards many other powerful optimizations
in python that have not yet been implemented or considered so this could just be the tip of the
iceberg now i should point out that these kinds of isolated examples are illustrations they're like
benchmarks in the sense that they don't really measure performance of real programs in the wild
they're just isolated examples that are used to show you how dramatic the differences can be
your own programs may not necessarily show these kinds of dramatic improvements when you run them
with 3.11 but they point towards how a lot has changed under the hood just this one version and
how many more changes are on the way too that's it for this video if you liked it please leave
a comment below don't forget to subscribe to the tech talk channel on youtube and for more
smart python and we're dev with serdar be sure to follow me on facebook youtube and infoworld.com