Optimized with numba, 312 times faster.