6

jott - py3.11_vs_3.8

 1 year ago
source link: https://jott.live/markdown/py3.11_vs_3.8
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python 3.11 is much faster than 3.8

by @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).

Simulated_trajectories_of_four_rocky_planets_with_dt_86400.gif

Fij=Gmimj(qj−qi)|qj−qi|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)

$ 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.

$ 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.

JavaScript isn't exactly an underdog in this comparison. It's more popular and reputably faster than Python.

But just how much faster? (JavaScript source code)

$ 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)

$ 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

$ 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 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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK