Why is indexing into an numpy array that slow?

Robert Kern robert.kern at gmail.com
Mon Nov 10 22:59:32 CET 2008

Rüdiger Werner wrote:
> Hello!


numpy questions are best answered on the numpy mailing list.


> Out of curiosity and to learn a little bit about the numpy package i've 
> tryed to implement
> a vectorised version of the 'Sieve of Zakiya'.
> While the code itself works fine it is astounding for me that the numpy 
> Version is almost 7 times slower than
> the pure python version. I tryed to find out if i am doing something wrong 
> but wasn't able to find any answer.

The result of indexing into a numpy array is a numpy scalar object. We do this 
instead of returning a float or an int because numpy supports many more data 
types than just a C double or long, respectively. If I have a uint16 array, 
indexing into it gives me a uint16 numpy scalar. These are a little more 
complicated to set up than a regular Python float or int, so they take more time 
to create.

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