[SciPy-user] Creating a Matrix from a Sum

David Warde-Farley dwf at cs.toronto.edu
Sat Jul 4 16:25:03 EDT 2009


> In [157]: x = randn(5000)
>
> In [158]: timeit numpy_impl(x)
> 1 loops, best of 3: 5.41 s per loop
>
> I did start running python_impl with len(x) = 5000 but my back of the
> envelope calculation is that it will be taking at least 1 hour per
> try, so timeit will be done in about 3 hours. :(


So, things didn't scale quite as badly as I thought they would (I was  
running on very little sleep, heh)

In [157]: x = randn(5000)

In [158]: timeit numpy_impl(x)
1 loops, best of 3: 5.41 s per loop

In [159]: timeit python_impl(x)
1 loops, best of 3: 1.01e+12 ns per loop

Why IPython gave me an answer in nanoseconds I'm not sure...  But  
that's about 17 minutes.

In either case,  then, you're looking at roughly a 200x speed up of  
the numpy version over the python loops, which is quite significant.  
If you can spare the  ~2N^2 intermediate memory usage, it's definitely  
worth it.

If you can't, but you still can't tolerate the speed hit, you can run  
the Python implementation Tom gave through Cython and add type  
specifiers for i, j, P and x, see http://docs.cython.org/docs/numpy_tutorial.html 
  - that should achieve similar speedups without the memory waste.

David



More information about the SciPy-User mailing list