extremely slow array indexing?

Robert Kern robert.kern at gmail.com
Thu Nov 30 13:50:50 EST 2006


John Machin wrote:
> Hi Grace,
> What Will McGugan said, plus:
> 1. Post *much* more of your code e.g. all relevant parts :-)

Note that Grace has also posted this to numpy-discussion and with prompting
provided the following snippet as a distillation of the key slow part:


from numpy import *

componentcount = 300000
currSum = zeros(componentcount)
row = zeros(componentcount) #current row
rowcount = 50000
for i in range(1,rowcount):
    row[:] = 1
    currSum = currSum + row;


As it is, the OP gets through 1000 rows every 7 seconds or so on their machine,
and I get about the same on mine.

Changing the last line to "currSum += row" gets a 3x speedup. Dropping the
"row[:] = 1" line as it's a really just a time-consuming no-op in the example
and probably not an accurate reflection of what's going on in the real code gets
you another 2x speedup.

-- 
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 Python-list mailing list