[SciPy-user] Python loops too slow

Robert Kern robert.kern at gmail.com
Wed Apr 8 23:06:26 EDT 2009


On Wed, Apr 8, 2009 at 22:02, Ross Williamson <Ross.Williamson at usap.gov> wrote:
> Ok so this is really annoying and slowing my code down.
>
> I've tried:
>
> nsamp = zeros(711)
> mask = ones(1000*1000)
> binned_all is (1000*1000 set of indices including numbers between 0 and 710)
>
> rbl = binned_ell.ravel()
> rmask = mask.ravel()
>
> nsamp[rbl] = nsamp[rbl] + mask
>
> And all it does is just add 1 to each element of nsamp - i.e. nsamp is
> now all ones rather than zeros and not the cumulative sum of the number
> of elements in binned_all (Note I'm using one here to make life easy -
> in reality it will be  a float of different values)

The reason why this can't work has been explained many times on this
list. The right way to do this is to use bincount().

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the SciPy-User mailing list