Skip to content

perf: 188 of 256 rows slower than NumPy 2.5.4 on one Zen 3 core (v0.41.0), by cause #118

Open

Description

@tannevaled

The first valid single-core timing of the whole public API against NumPy (go-ndarray/ndarray at v0.41.0, de3f41ac): 188 of 256 rows are slower than NumPy, 54 are faster, the rest at parity. Every slower row is an open defect under the performance rule. This issue tracks them by cause instead of one issue per row; each cause gets its own issue when work starts on it, and this list is ticked as they close.

Measurement: cfarm420 (AMD EPYC 7773X, Zen 3), one pinned core, GOMAXPROCS=1. The reference was NumPy 2.5.4, SciPy 1.18.1 and scipy-openblas 0.3.34 at one thread, Python 3.12.12 through uv. The 1-minute load over the rounds was 4.12 to 5.56, against a gate of 8. The harness was bench/ (go test -run TestPerf -perf.run, refbench v0.3.0), the data was identical on both sides, and the table is in docs/perf-table.md (PR to follow).

Causes, from the table

  • Result allocation. Zeros is 3.95× slower than np.zeros(100000), and Ones/Full 7–8×. The allocating Add/Sub/Mul are about 3× slower, while AddInto ties (1.03) and SubInto/MulInto are 1.06–1.09×: the kernel is at parity or close to it, and allocating and initialising the 800 kB result costs the rest. This one cause multiplies most allocating rows below.
  • Elementwise operations without a vector kernel. Ceil/Floor are 19–20× slower, Round 14×, Abs/Neg/Square 10–12×, the scalar forms (AddScalar…DivScalar) 5–9×, and ArgMax/ArgMin 5–8×. Maximum/Minimum, the comparisons and the logical operations are 3–5×, and Power and Log2 3.7–5×. These belong in go-simd/floats, one implementation (K1 remainder: float32 kernels, Dot1D and the dot inside GEMM, Maximum/Minimum move to go-simd/floats #98).
  • Copies where NumPy returns a view or wraps. FromData/FromSlice are 1100–1300× slower (NumPy's asarray wraps; the zero-copy constructor is Wrap: an array over a caller's buffer with given strides (zero-copy, for Julia column-major arrays) #76). Ravel and Real are 566× (views in NumPy), Flatten/Copy/AsType 4×, and Concatenate/Stack 3.5–3.7×.
  • linalg. At 64×64, the routines are 1.4–2.7× slower than LAPACK through NumPy and Norm 4.2× (perf: linalg SVD, Pinv, Lstsq, Eigh, QR, Eig allocate 90-276 times per 64x64 call #104 for the allocations).
  • fft. FFTShift/IFFTShift are 8–10× slower, and RFFT2/IRFFT2/IHFFT 1.2–1.5×. The complex transforms are faster (fft.FFT 0.55).
  • random. The draws built on log/exp are slower: Geometric 11.7×, Pareto 6.6×, Logseries 6.2×, Power 5.2×, Gumbel/Weibull 3.5×. The legacy RandomState/np.random.* forms are 2.1–2.8× across the board, and Choice 2.9–3.2×. Binomial, Gamma, Wald and Dirichlet are within 7% (1.02–1.07), but the harness still counts them slower.

Faster than NumPy (excerpt): SqrtInto 0.51, fft.FFT 0.54, Sum 0.70, Exp 0.89.

⚠ Claims elsewhere that this table contradicts. The landing (go-ndarray.github.io layouts/index.html) quotes multicore figures: "2.3–4.5×" on Sum/Mean/Max and "3.7–5.9×" over one-thread OpenBLAS. On one core, Max is 1.13× slower and MatMul 128² 1.15× slower. docs/plan-ndarray.md:102 says "beats NumPy on its core ops". These are corrected in their own PRs.

Refs #98, #104, #76

Slower rows: 188 of the table's rows, grouped by family, worst ratio first (ratio = Go / NumPy).

Array (allocating) (57)

case reference Go ns NumPy ns ratio
Array.Ravel numpy lambda x: x.ravel() 9.987e+04 176.2 566.78
Array.Real numpy np.real 9.257e+04 163.6 565.94
Array.Ceil numpy np.ceil 4.788e+05 2.353e+04 20.35
Array.Floor numpy np.floor 4.512e+05 2.349e+04 19.21
Array.Round numpy np.round 3.451e+05 2.498e+04 13.82
Array.Abs numpy np.abs 2.746e+05 2.3e+04 11.94
Array.Square numpy np.square 2.792e+05 2.537e+04 11.00
Array.Neg numpy np.negative 2.489e+05 2.421e+04 10.28
Array.AddScalar numpy x + 1.5 2.042e+05 2.385e+04 8.56
Array.SubScalar numpy x - 1.5 2.077e+05 2.443e+04 8.50
Array.MulScalar numpy x * 1.5 2.056e+05 2.582e+04 7.96
Array.ArgMaxAxis/axis1 numpy np.argmax(axis=1) 1.885e+05 2.415e+04 7.81
Array.ArgMinAxis/axis1 numpy np.argmin(axis=1) 1.842e+05 2.426e+04 7.59
Array.ArgMax numpy np.argmax 9.802e+04 1.85e+04 5.30
Array.ArgMin numpy np.argmin 9.665e+04 1.824e+04 5.30
Array.DivScalar numpy x / 1.5 2.038e+05 3.861e+04 5.28
Array.Power numpy lambda x: np.power(x, 2.5) 7.48e+06 1.455e+06 5.14
Array.Maximum numpy np.maximum 1.687e+05 3.608e+04 4.68
Array.All numpy np.all 1.087e+05 2.543e+04 4.28
Array.Flatten numpy lambda x: x.flatten() 9.501e+04 2.352e+04 4.04
Array.Copy numpy lambda x: x.copy() 9.669e+04 2.411e+04 4.01
Array.AsType numpy lambda x: x.astype(np.float32) 7.646e+04 1.907e+04 4.01
Array.Minimum numpy np.minimum 1.407e+05 3.518e+04 4.00
Array.Imag numpy np.imag 4.966e+04 1.307e+04 3.80
Array.Log2 numpy np.log2 1.752e+06 4.75e+05 3.69
Array.LogicalOr numpy np.logical_or 3.632e+05 1.019e+05 3.56
Array.LogicalAnd numpy np.logical_and 3.597e+05 1.04e+05 3.46
Array.Any numpy np.any 8.08e+04 2.393e+04 3.38
Array.Sub numpy np.subtract 1.177e+05 3.539e+04 3.33
Array.Conj numpy np.conj 1.102e+05 3.354e+04 3.29
Array.NotEqual numpy np.not_equal 8.189e+04 2.49e+04 3.29
Array.ProdAxis/axis1 numpy np.prod(axis=1) 3.793e+05 1.17e+05 3.24
Array.Equal numpy np.equal 7.875e+04 2.532e+04 3.11
Array.Greater numpy np.greater 7.763e+04 2.51e+04 3.09
Array.LogicalXor numpy np.logical_xor 3.545e+05 1.146e+05 3.09
Array.Add numpy np.add 1.13e+05 3.668e+04 3.08
Array.GreaterEqual numpy np.greater_equal 7.696e+04 2.515e+04 3.06
Array.Less numpy np.less 7.547e+04 2.472e+04 3.05
Array.Mul numpy np.multiply 1.072e+05 3.515e+04 3.05
Array.LessEqual numpy np.less_equal 7.447e+04 2.447e+04 3.04
Array.Div numpy np.divide 1.11e+05 4.064e+04 2.73
Array.Clip numpy np.clip 9.356e+04 3.706e+04 2.52
Array.Outer numpy np.outer, 1000 x 1000 1.536e+06 6.17e+05 2.49
Array.Nonzero numpy np.flatnonzero 5.714e+05 2.456e+05 2.33
Array.LogicalNot numpy np.logical_not 1.452e+05 7.767e+04 1.87
Array.ArgMinAxis/axis0 numpy np.argmin(axis=0) 1.66e+05 9.956e+04 1.67
Array.Angle numpy np.angle 9.532e+05 5.805e+05 1.64
Array.MaxAxis/axis0 numpy np.max(axis=0) 1.04e+05 6.362e+04 1.63
Array.ArgMaxAxis/axis0 numpy np.argmax(axis=0) 1.669e+05 1.028e+05 1.62
Array.MaskSelect numpy x[mask] (mask x > 2 built outside the call) 4.133e+05 3.251e+05 1.27
Array.MatMul numpy np.matmul, 128 x 128 (OpenBLAS, one thread) 1.232e+05 1.072e+05 1.15
Array.MinAxis/axis0 numpy np.min(axis=0) 7.179e+04 6.377e+04 1.13
Array.Max numpy np.max 1.874e+04 1.665e+04 1.13
Array.Cos numpy np.cos 1.158e+06 1.046e+06 1.11
Array.Dot numpy np.dot, 128 x 128 (OpenBLAS, one thread) 1.172e+05 1.063e+05 1.10
Array.Min numpy np.min 1.789e+04 1.642e+04 1.09
Array.Sin numpy np.sin 1.132e+06 1.058e+06 1.07

Array (preallocated output) (2)

case reference Go ns NumPy ns ratio
Array.MulInto numpy np.multiply (out= preallocated) 3.85e+04 3.53e+04 1.09
Array.SubInto numpy np.subtract (out= preallocated) 3.792e+04 3.594e+04 1.06

constructors and package functions (18)

case reference Go ns NumPy ns ratio
FromData numpy np.asarray (wraps, no copy) 8.163e+04 61.79 1321.18
FromSlice numpy np.asarray (wraps, no copy) 7.216e+04 61.48 1173.84
Full numpy np.full(100000, 1.5) 1.543e+05 1.928e+04 8.00
Ones numpy np.ones(100000) 1.412e+05 1.923e+04 7.34
FullOf numpy np.full(100000, 1.5, dtype=np.float32) 6.461e+04 1.043e+04 6.19
OnesOf numpy np.ones(100000, dtype=np.float32) 5.985e+04 1.085e+04 5.51
ZerosOf numpy np.zeros(100000, dtype=np.float32) 3.308e+04 6058 5.46
New numpy np.zeros(100000) 5.19e+04 1.305e+04 3.98
Zeros numpy np.zeros(100000) 5.194e+04 1.315e+04 3.95
Concatenate numpy np.concatenate 1.84e+05 4.913e+04 3.74
HStack numpy np.hstack 1.914e+05 5.201e+04 3.68
VStack numpy np.vstack 1.878e+05 5.117e+04 3.67
Stack numpy np.stack 1.797e+05 5.117e+04 3.51
Arange numpy np.arange(0.0, 100000.0, 1.0) 2.113e+05 6.121e+04 3.45
Eye numpy np.eye(512) 1.54e+05 4.583e+04 3.36
Identity numpy np.identity(512) 1.428e+05 4.483e+04 3.19
Linspace numpy np.linspace(0.0, 1.0, 100000) 2.159e+05 9.03e+04 2.39
Where numpy np.where (mask x > 2 built outside the call) 1.51e+05 7.427e+04 2.03

fft (10)

case reference Go ns NumPy ns ratio
fft.IFFTShift numpy np.fft.ifftshift 2.965e+05 3.061e+04 9.69
fft.FFTShift numpy np.fft.fftshift 2.703e+05 3.215e+04 8.41
fft.RFFTFreq numpy np.fft.rfftfreq 6.206e+04 3.486e+04 1.78
fft.FFTFreq numpy np.fft.fftfreq 1.307e+05 8.531e+04 1.53
fft.RFFT2With numpy np.fft.rfft2(norm='ortho') 4.681e+05 3.123e+05 1.50
fft.RFFT2 numpy np.fft.rfft2 4.06e+05 2.879e+05 1.41
fft.IRFFT2 numpy np.fft.irfft2 4.27e+05 3.049e+05 1.40
fft.IHFFT numpy np.fft.ihfft 6.302e+05 4.728e+05 1.33
fft.IHFFTWith numpy np.fft.ihfft(norm='ortho') 5.814e+05 4.777e+05 1.22
fft.IRFFT2With numpy np.fft.irfft2(norm='ortho') 3.733e+05 3.122e+05 1.20

linalg (20)

case reference Go ns NumPy ns ratio
linalg.Norm numpy np.linalg.norm(a) 8481 2029 4.18
linalg.Inv numpy np.linalg.inv(a) 1.69e+05 6.384e+04 2.65
linalg.LstsqRcond numpy np.linalg.lstsq(a, b, rcond=1e-10) 1.174e+06 5.237e+05 2.24
linalg.Slogdet numpy np.linalg.slogdet(a) 6.36e+04 2.84e+04 2.24
linalg.Solve numpy np.linalg.solve(a, b) 7.826e+04 3.542e+04 2.21
linalg.Det numpy np.linalg.det(a) 5.683e+04 2.589e+04 2.19
linalg.Lstsq numpy np.linalg.lstsq(a, b, rcond=None) 1.163e+06 5.38e+05 2.16
linalg.Cholesky numpy np.linalg.cholesky(a) 3.932e+04 1.847e+04 2.13
linalg.SVDVals numpy np.linalg.svd(a, compute_uv=False) 5.736e+05 2.733e+05 2.10
linalg.Cond numpy np.linalg.cond(a) 5.819e+05 2.767e+05 2.10
linalg.MatrixRankTol numpy np.linalg.matrix_rank(a, tol=1e-8) 5.761e+05 2.844e+05 2.03
linalg.SVD numpy np.linalg.svd(a, full_matrices=False) 1.148e+06 5.676e+05 2.02
linalg.MatrixRank numpy np.linalg.matrix_rank(a) 5.724e+05 2.88e+05 1.99
linalg.Pinv numpy np.linalg.pinv(a) 1.178e+06 6.207e+05 1.90
linalg.PinvRcond numpy np.linalg.pinv(a, rcond=1e-10) 1.176e+06 6.295e+05 1.87
linalg.Eigh numpy np.linalg.eigh(a) 6.148e+05 3.307e+05 1.86
linalg.QR numpy np.linalg.qr(a) 2.331e+05 1.401e+05 1.66
linalg.Eigvalsh numpy np.linalg.eigvalsh(a) 2.551e+05 1.541e+05 1.66
linalg.Eigvals numpy np.linalg.eigvals(a) 7.234e+05 4.6e+05 1.57
linalg.Eig numpy np.linalg.eig(a) 1.37e+06 9.541e+05 1.44

random (81)

case reference Go ns NumPy ns ratio
random.Generator.Geometric numpy Generator.geometric(0.2, 100000) 1.836e+07 1.567e+06 11.72
random.Generator.Pareto numpy Generator.pareto(3.0, 100000) 1.316e+07 1.994e+06 6.60
random.Generator.Logseries numpy Generator.logseries(0.6, 100000) 2.802e+07 4.527e+06 6.19
random.Generator.Power numpy Generator.power(3.0, 100000) 2.251e+07 4.354e+06 5.17
random.Generator.Gumbel numpy Generator.gumbel(0.0, 1.0, 100000) 7.296e+06 1.999e+06 3.65
random.Generator.Weibull numpy Generator.weibull(1.5, 100000) 7.779e+06 2.241e+06 3.47
random.Generator.Choice numpy Generator.choice(x, 100000) 1.248e+06 3.919e+05 3.18
random.Generator.Logistic numpy Generator.logistic(0.0, 1.0, 100000) 3.847e+06 1.293e+06 2.98
random.Choice numpy np.random.choice(x, 100000) 1.397e+06 4.751e+05 2.94
random.Generator.Zipf numpy Generator.zipf(2.5, 100000) 3.238e+07 1.126e+07 2.87
random.RandomState.Choice numpy RandomState.choice(x, 100000) 1.478e+06 5.179e+05 2.85
random.RandomState.StandardExponential numpy RandomState.standard_exponential(100000) 3.924e+06 1.399e+06 2.80
random.RandomState.ChoiceN numpy RandomState.choice(1000, 100000) 9.796e+05 3.715e+05 2.64
random.RandomState.Exponential numpy RandomState.exponential(1.5, 100000) 3.934e+06 1.505e+06 2.61
random.Generator.ChoiceN numpy Generator.choice(1000, 100000) 8.331e+05 3.316e+05 2.51
random.RandomState.Randint numpy RandomState.randint(0, 1000, 100000) 8.859e+05 3.665e+05 2.42
random.Randint numpy np.random.randint(0, 1000, 100000) 8.788e+05 3.681e+05 2.39
random.RandomState.Lognormal numpy RandomState.lognormal(0.0, 1.0, 100000) 7.612e+06 3.254e+06 2.34
random.RandomState.RandomSample numpy RandomState.random_sample(100000) 1.133e+06 4.95e+05 2.29
random.RandomState.Rand numpy RandomState.rand(100000) 1.14e+06 4.991e+05 2.28
random.RandomSample numpy np.random.random_sample(100000) 1.16e+06 5.098e+05 2.28
random.Rand numpy np.random.rand(100000) 1.144e+06 5.137e+05 2.23
random.RandomState.Randn numpy RandomState.randn(100000) 4.445e+06 1.995e+06 2.23
random.RandomState.StandardNormal numpy RandomState.standard_normal(100000) 4.436e+06 2e+06 2.22
random.Randn numpy np.random.randn(100000) 4.443e+06 2.004e+06 2.22
random.RandomState.FillIntN numpy RandomState.randint(0, 1000, 100000) (allocates; Go fills dst) 8.077e+05 3.653e+05 2.21
random.RandomState.Normal numpy RandomState.normal(0.0, 1.0, 100000) 4.536e+06 2.086e+06 2.18
random.Normal numpy np.random.normal(0.0, 1.0, 100000) 4.551e+06 2.097e+06 2.17
random.RandomState.Uniform numpy RandomState.uniform(0.0, 1.0, 100000) 1.258e+06 6.031e+05 2.09
random.Generator.IntegersOf numpy Generator.integers(0, 1000, 100000, dtype=np.int32) 6.741e+05 3.229e+05 2.09
random.Uniform numpy np.random.uniform(0.0, 1.0, 100000) 1.234e+06 5.928e+05 2.08
random.Generator.Lognormal numpy Generator.lognormal(0.0, 1.0, 100000) 3.9e+06 1.911e+06 2.04
random.Generator.Multinomial numpy Generator.multinomial(100, [0.2, 0.3, 0.5], 20000) 1.286e+07 6.642e+06 1.94
random.Generator.IntegersUint64 numpy Generator.integers(0, 1000, 100000, dtype=np.uint64) 6.152e+05 3.202e+05 1.92
random.Generator.Laplace numpy Generator.laplace(0.0, 1.0, 100000) 3.767e+06 1.967e+06 1.91
random.Generator.Integers numpy Generator.integers(0, 1000, 100000) 6.065e+05 3.178e+05 1.91
random.Poisson numpy np.random.poisson(5.0, 100000) 1.127e+07 6.081e+06 1.85
random.RandomState.Poisson numpy RandomState.poisson(5.0, 100000) 1.127e+07 6.101e+06 1.85
random.Generator.Bytes numpy Generator.bytes(1000000) 1.186e+06 6.643e+05 1.79
random.Generator.Uniform numpy Generator.uniform(0.0, 1.0, 100000) 7.438e+05 4.309e+05 1.73
random.Generator.RandomOf numpy Generator.random(100000, dtype=np.float32) 4.155e+05 2.453e+05 1.69
random.Generator.StandardExponentialOf numpy Generator.standard_exponential(100000, dtype=np.float32, method='zig') 8.741e+05 5.163e+05 1.69
random.RandomState.ChiSquare numpy RandomState.chisquare(3.0, 100000) 6.84e+06 4.125e+06 1.66
random.Generator.FillIntN numpy Generator.integers(0, 1000, 100000) (allocates; Go fills dst) 5.15e+05 3.148e+05 1.64
random.RandomState.StandardGamma numpy RandomState.standard_gamma(2.0, 100000) 6.664e+06 4.08e+06 1.63
random.RandomState.FillUniform numpy RandomState.uniform(0, 1, 100000) (allocates; Go fills dst) 9.737e+05 6.042e+05 1.61
random.RandomState.Gamma numpy RandomState.gamma(2.0, 1.5, 100000) 6.687e+06 4.175e+06 1.60
random.Generator.Permutation numpy Generator.permutation(100000) 1.971e+06 1.24e+06 1.59
random.RandomState.RandintOf numpy RandomState.randint(0, 1000, 100000, dtype=np.int32) 5.759e+05 3.623e+05 1.59
random.RandomState.Bytes numpy RandomState.bytes(1000000) 1.451e+06 9.234e+05 1.57
random.RandomState.Shuffle numpy RandomState.shuffle (in place, on a copy) 1.935e+04 1.237e+04 1.56
random.Generator.Poisson numpy Generator.poisson(5.0, 100000) 7.868e+06 5.08e+06 1.55
random.Permutation numpy np.random.permutation(100000) 2.06e+06 1.342e+06 1.54
random.Shuffle numpy np.random.shuffle (in place, on a copy) 1.939e+04 1.27e+04 1.53
random.RandomState.Permutation numpy RandomState.permutation(100000) 2.084e+06 1.387e+06 1.50
random.RandomState.PermutationOf numpy RandomState.permutation(x) 1.977e+04 1.317e+04 1.50
random.Generator.VonMises numpy Generator.vonmises(0.0, 2.0, 100000) 1.27e+07 8.554e+06 1.48
random.Generator.StandardExponential numpy Generator.standard_exponential(100000) 8.541e+05 5.82e+05 1.47
random.Generator.NegativeBinomial numpy Generator.negative_binomial(5.0, 0.5, 100000) 1.106e+07 7.547e+06 1.47
random.Generator.Shuffle numpy Generator.shuffle (in place) 1.739e+04 1.193e+04 1.46
random.Generator.Random numpy Generator.random(100000) 4.039e+05 2.906e+05 1.39
random.Generator.PermutationOf numpy Generator.permutation(x, axis=0) 1.741e+04 1.3e+04 1.34
random.Generator.PermutedAxis numpy Generator.permuted(x, axis=0) 1.541e+04 1.168e+04 1.32
random.RandomState.Multinomial numpy RandomState.multinomial(100, [0.2, 0.3, 0.5], 20000) 8.452e+06 6.506e+06 1.30
random.Generator.Triangular numpy Generator.triangular(0.0, 1.0, 3.0, 100000) 1.597e+06 1.262e+06 1.27
random.RandomState.ShuffleInt64 numpy RandomState.shuffle of 100000 int64 (in place) 1.635e+06 1.288e+06 1.27
random.Generator.Exponential numpy Generator.exponential(1.5, 100000) 9.802e+05 7.839e+05 1.25
random.Generator.Rayleigh numpy Generator.rayleigh(1.5, 100000) 9.639e+05 7.818e+05 1.23
random.Generator.Permuted numpy Generator.permuted(x) 1.487e+04 1.245e+04 1.19
random.Generator.ChiSquare numpy Generator.chisquare(3.0, 100000) 2.911e+06 2.438e+06 1.19
random.Generator.StandardGammaOf numpy Generator.standard_gamma(2.0, 100000, dtype=np.float32) 2.22e+06 1.886e+06 1.18
random.Generator.ShuffleInt64 numpy Generator.shuffle of 100000 int64 (in place) 1.375e+06 1.187e+06 1.16
random.RandomRaw numpy PCG64.random_raw(100000) 3.409e+05 2.936e+05 1.16
random.Generator.StandardGamma numpy Generator.standard_gamma(2.0, 100000) 2.362e+06 2.129e+06 1.11
random.Generator.F numpy Generator.f(5.0, 10.0, 100000) 5.548e+06 5.064e+06 1.10
random.RandomState.Binomial numpy RandomState.binomial(100, 0.3, 100000) 1.962e+07 1.831e+07 1.07
random.Generator.Dirichlet numpy Generator.dirichlet([1.0, 2.0, 3.0], 20000) 1.303e+06 1.222e+06 1.07
random.Generator.Wald numpy Generator.wald(1.0, 2.0, 100000) 3.75e+06 3.542e+06 1.06
random.Binomial numpy np.random.binomial(100, 0.3, 100000) 1.913e+07 1.823e+07 1.05
random.Generator.Gamma numpy Generator.gamma(2.0, 1.5, 100000) 2.908e+06 2.8e+06 1.04
random.Generator.Binomial numpy Generator.binomial(100, 0.3, 100000) 1.853e+07 1.82e+07 1.02

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions