Four languages, three kernels
200000 elements, best of 7, in one process through ctypes. Log axis.
| kernel | pure Python (ns) | NumPy (ns) | C, restrict (ns) |
|---|---|---|---|
| saxpy y = a*x + y | 37.79 | 0.29 | 0.13 |
| magnitude sqrt(ax^2 + ay^2) | 76.75 | 1.31 | 0.34 |
| poly five chained multiply-adds | 72.96 | 1.13 | 0.14 |
How this was measured
Everything ran in one process on the same arrays. Pure Python and NumPy
ran directly. C and Prestissimo were built into shared libraries and called
through ctypes, which is how a real program would call them.
Each figure is the best of 7 runs.
The axis is logarithmic because the range spans about four orders of magnitude. On a linear axis every compiled bar would be a single pixel.
What it does and does not show
The gap between Python and the compiled languages is the cost of an interpreter, and it is not news. The interesting comparison is the one between the last two bars, which are usually within a few percent of each other: both go through the same LLVM back end, so parity is the ceiling rather than a disappointment.
NumPy sits between them. It runs the same vector instructions, but pays
for a pass over memory per operation rather than fusing the whole
expression into one pass, which is why the gap widens on
poly.
Timings on a desktop vary by tens of percent between runs. Read the orders of magnitude, not the last digit.