3 ms·
Can someone explain why the numpy code ends up being so slow? Is the author actually doing things in a vectorized manner or just looping over np arrays instead
by eachro 3y ago
Can someone explain why the numpy code ends up being so slow? Is the author actually doing things in a vectorized manner or just looping over np arrays instead of lists? It's hard to tell from the info given.
- hansvm 3y ago> Here’s a tip: If you’re using NumPy, go all in. Mixing it with regular Python can really slow you down! It looks like they shoved numpy arrays into the place native Python containers used to occupy. They incurred more of a slowdown than you might naively have expected from that particular obvious error. Unrelated to this article, numpy is often a lot slower than expected even when used "correctly." Major culprits include order of operations (compared to the ideal / forced by the API / when you think vectorization automagically makes a bad order of operations fast), allocations, memory bandwidth, kernels optimized for a dimension other than the one you care about, kernels optimized for a use case other than the one you care about, and improper installation (appropriately linking fast BLAS/LAPACK objects). Most of those sins are rectifiable without any major change in coding habits by wrapping your nightmares in `jax.jit` or `torch.compile`, but if your devs are writing the sort of code that makes those things _necessary_ (rather than just nice-to-haves) then that probably still won't solve your problems.