Python 3.7 is in Beta! It’s time to get testing… is it any faster?

Here are the major speed boosts you’ll get with Python 3.7 versus 3.6

Warning: some of the topics in this article are quite detailed and beyond the level I normally blog at. If you don’t know some of the terms or meanings, just download and explore the examples — keep reading and playing!

Time for some go faster stripes..

1. Calling methods faster (maybe)

The title of this change is “Speedup method calls 1.2x”, which is a bit misleading..

There are many ways to change CPython, either by modifying the execution of the Opcode, or by adding new Opcodes. Adding new Opcodes requires a lot of discussion and testing and this change introduces new Opcodes. Opcodes are selected by the compilation process in CPython. Once your code is converted into an Abstract-Syntax-Tree, the compiler explores each branch and converts it to Opcodes. The execution of your code goes through the Opcodes in a massive switch statement inside a loop and calls different C-functions for each opcode.

For reference, Python 3.6 has 3 Opcodes for calling functions. All of these were either added or modified in Python 3.6.

For calling positional argument-only functions: CALL_FUNCTION ,

, For calling positional and keyword functions: CALL_FUNCTION_KW ,

, For calling variable positional or keyword functions: CALL_FUNCTION_EX

Python 3.7 adds 2 new Opcodes, LOAD_METHOD and CALL_METHOD for when the compiler sees x.method(...) it uses these new Opcodes.

As an example calling 3 functions with different signatures:

Running this on Python 3.6 and Python 3.7 we can see no change in the resulting code, or performance.

Another example with bound methods (ie those belonging to an instance of a class),

The results of this show: