When NumPy is too slow
If you’re doing numeric calculations, NumPy is a lot faster than than plain Python—but sometimes that’s not enough.
What should you do when your NumPy-based code is too slow?
Your first thought might be parallelism, but that should probably be the last thing you consider.
There are many speedups you can do before parallelism becomes helpful, from algorithmic improvements to working around NumPy’s architectural limitations.
Let’s see why NumPy can be slow, and then some solutions to help speed up your code even more.
Step #1: Before you optimize, choose a scalable algorithm
Before you start too much time thinking about speeding up your NumPy code, it’s worth making sure you’ve picked a scalable algorithm.
An O(N)
algorithm will scale much better than O(N2)
; the