by kristianp · 8mo ago · 0 comments
nbody spectral-norm
C 2100ms 400ms
Graal 211ms 212ms
PyPy 98ms 1065ms
Seeing Graal and Pypy beat the gcc C versions suggests to me there's something wrong with the C version. Perhaps they need a -march=native or there's something else wrong. The C version would be a different implementation in the benchmark game, but usually they are highly optimised.Edit: looking at [1] the top C version uses x86 intrinsics, perhaps the article's writer had to find a slower implementation to have it running natively on his M4 Pro? It would be good to know which C version he used, there's a few at [1]. The N-body benchmark is one where they specify that the same algorithm must be used for all implementations.
[1] https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
Obviously the main point of the article is to compare different python optimisations, however "rewrite it in C/C++/rust/Go" is an option that should be considered, and none of his optimisations on his M4 Pro beat the C option on my 6-year old M1 mac mini.
The rest of the numbers in the blog post use a 500k iterations for the nbody simulation. Here's my numbers on the M1 mac using the default Clang installed with xcode:
Clang 17.0.0. 0.06s
python 3.12.11 1.59s
pypy 3.11.13. 0.23s
I used the fastest C code that doesn't use intrinsics at [1] and compiled withclang -O3 -march=native nbody-gcc-6.c -o nbody.clang6
Used the python version from [2].
[1] http://benchmarksgame-team.pages.debian.net/benchmarksgame/p...
[2] https://github.com/cemrehancavdar/faster-python-bench/tree/m...
https://benchmarksgame-team.pages.debian.net/benchmarksgame/...