Get excited about Python 3.11 – It's finally the time for significant performance improvements
It's no secret that Python isn't the fastest programming language out there. Well, that's about to change, or at least head in the right direction. The newest Python release – Python 3.11 – is expected to air in October 2022. What's even better is there's a release candidate (RC) version available for us to play around with.
That's exactly what we'll do today. We'll install Python 3.10 and 3.11 in separate Docker containers and see how they compare in a suite of benchmark tests. I plan to use the pyperformance package for the job, as it will do all the heavy lifting.
TL;DR – On average, Python 3.11 is 14% faster than Python 3.10. The new version is marginally slower on some benchmarks, but on the others, it's up to 64% faster. I ran the benchmarks on M1 Pro MacBook Pro 16 with a 10-core CPU. Each Python version was installed in Docker, which utilized 5 logical CPU cores. Your mileage may vary, but you should see similar results, relatively speaking.
Don't feel like reading? Watch my video instead:
How to Install Python 3.11 in Docker
If you want to follow along, you'll need Docker installed. It's a must-have tool in any data science tool belt, so it shouldn't be an issue. Once Docker is running, open two Terminal tabs.
In the first, we'll fetch and run Python 3.10 in the background:
docker run -t -d python:3.10.4-bullseye
And in the second we'll do the same thing but for Python 3.11:
docker run -t -d python:3.11-rc-bullseye
Docker will take some time to download and start both images, depending on your hardware configuration and Internet speed. Once done, you can open two Visual Studio Code windows and attach them to the Docker containers (Use the Docker VSCode extension, right-click on the image, and select "Attach Visual Studio Code"). Alternatively, you could also attach the shell only.
Once in the container, launch a new VSCode integrated Terminal and check the Python versions:
As you can see, I have Python 3.10 on the left and Python 3.11 on the right. Up next, we'll install the
pyperformance package and run the benchmarks.
How to Run Python Benchmarks with PyPerformance
Okay, so we have both containers running and attached in VSCode. That was the hard part.
The next step is to install the
pyperformance package. Run the following command in both containers:
python3 -m pip install pyperformance
Once installed, run the below shell command in the VSCode window attached to Python 3.10 container:
pyperformance run -o py310.json
And run a similar command in Python 3.11 container:
pyperformance run -o py311.json
These commands will run a suite of couple dozen benchmarks, so go grab a cup of coffee – it will take some time.
pyperformance will save the output to
py311.json files, respectively:
Each file is in a separate Docker container, so to actually compare the two, you'll have to download the JSON files to your local machine (or upload the file from one container to the other).
I've downloaded them both locally and installed the pyperformance package in a Python virtual environment. Let's see how they compare next.
Python 3.10 vs. Python 3.11 – Which One is Faster?
Preferably, you'll want to put both JSON files in the same folder. Open that folder in a Terminal window and run the following shell command:
pyperf compare_to py310.json py311.json --table
Here's the output:
I count five tests in which Python 3.10 was marginally faster than Python 3.11. In others, Python 3.11 was up to 64% faster than Python 3.10. According to the geometric mean, Python 3.11 is 14% faster than Python 3.10.
Summary of Python 3.10 vs. Python 3.11 Benchmarks
And there you have it – performance comparison between Python 3.10 and Python 3.11. It's worth noting that Python 3.11 still isn't out yet, so we're comparing a fully stable release with a release candidate. Maybe the gap will be even bigger between the two after the official release. Only time will tell.
What do you think about the upcoming Python release? Are you excited to see Python running faster, or you don't care at all? Let me know in the comment section below.
- 5 Best Books to Learn Data Science Prerequisites (Math, Stats, and Programming)
- Top 5 Books to Learn Data Science in 2022
- 7 Ways to Print a List in Python