# Python 3.11 is *much* faster than 3.8

*by [@bwasti](https://twitter.com/bwasti)*

****

This is awesome, but let's not get ahead of ourselves.

Running on an M1 Pro, I tried some N-body simulation code
across Python 3.8, 3.11, Bun and C++17 (-O2).

<center>
<img width=80% style='max-width:400px' src=https://upload.wikimedia.org/wikipedia/commons/7/7a/Simulated_trajectories_of_four_rocky_planets_with_dt_86400.gif>
</center>

$$
\mathbf{F}_{ij} = \frac{G m_i m_j \left(\mathbf{q}_j - \mathbf{q}_i\right)}{\left\| \mathbf{q}_j - \mathbf{q}_i\right\|^3}
$$

### Python

Python is a popular but reputably slow interpreted language.
It's been making strides in performance and the most recent release
shows a noticable speedup on my system.

For this test, I ran 10M steps of an N-body simulation.
([Python source code](https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/nbody-python3-1.html))

```bash
$ time python3.8 sim.py 10000000
-0.169075164
-0.169077842
python3.8 sim.py 10000000  96.07s user 0.63s system 99% cpu 1:36.79 total
```
Python 3.8 took **96.79 seconds**.

```bash
$ time python3.11 sim.py 10000000
-0.169075164
-0.169077842
python3.11 sim.py 10000000  31.92s user 0.05s system 99% cpu 31.976 total
```

Python 3.11 took only **31.98 seconds**!  That's 3x faster!

### JavaScript

JavaScript is a good baseline for language
performance.
Just like Python, it's a flexible dynamically typed scripting language,
can be run in the terminal, and has a [well established
C-API](https://nodejs.org/api/n-api.html).

JavaScript isn't exactly an underdog in this comparison.
It's [more popular](https://survey.stackoverflow.co/2022/#section-most-popular-technologies-programming-scripting-and-markup-languages)
and reputably faster than Python.

But just how much faster?
([JavaScript source code](https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/nbody-node-6.html))

```bash
$ time bun sim.ts 10000000
-0.169075164
-0.169077842
bun sim.ts 10000000  0.76s user 0.01s system 100% cpu 0.768 total
```

Bun took **0.768 seconds** to run the simulation, 41x faster than Python 3.11.
To anyone familiar with JavaScript,
this is pretty much expected.  It's a JIT compiled language with
far more investment and multiple competitive runtimes.

### C++17

C++ is a compiled language, which means that it lacks some of the
convenience of Python and JavaScript.
Besides strict typing and having a generally ugly syntax,
C++ also requires ahead of time compilation.

It takes **0.183 seconds** to compile the C++ code with -O2.
([C++ source code](https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/nbody-gpp-9.html))

```bash
$ time g++ -O2 -std=c++17 sim.cc -o sim
g++ -O2 -std=c++17 sim.cc -o sim  0.11s user 0.03s system 76% cpu 0.183 total
```

Then, running the binary

```bash
$ time ./sim 10000000
-0.169075164
-0.169077842
./sim 10000000  0.42s user 0.00s system 99% cpu 0.423 total
```

C++ takes **0.423 seconds** to run the simulation,
1.8x faster than JavaScript.
To me, this number represents the "peak" performance a language like Python
could hit.

JIT compiled languages can theoretically
beat out compiled languages.
As far as I know, this hasn't really happened in practice.

### Go Python, Go!

I think it's awesome Python is improving performance.
The language has very nice syntax and is often the first language
taught to new programmers.

There are some really cool efforts to JIT compile CPython,
and I'd like to highlight [Torch Dynamo](https://github.com/pytorch/torchdynamo) as one of them.
Although it is used for PyTorch code, the technique is a generally applicable
bytecode rewriting process that's worth checking out.