[Numpy-discussion] Performance of the array protocol

Travis Oliphant oliphant at ee.byu.edu
Tue Nov 1 08:13:06 CST 2005


Francesc Altet wrote:

>Hi,
>
>I'm trying to start using the array protocol for conversion between
>Numeric <--> numarray (and newcore in the future), but I'm a bit
>disappointed because of its performance. For numarray --> Numeric we
>have:
>
>  
>
>>>>t1=timeit.Timer("num=Numeric.array(na)", "import numarray; import
>>>>        
>>>>
>Numeric; na=numarray.arange(10)")
>  
>
>>>>t1.repeat(3,10000)
>>>>        
>>>>
>[0.59375977516174316, 0.57908082008361816, 0.56574010848999023]
>  
>
>>>>t2=timeit.Timer("num=Numeric.fromstring(na._data,typecode=na.typecode())", "import numarray; import Numeric; na=numarray.arange(10)")
>>>>t2.repeat(3,10000)
>>>>        
>>>>
>[0.11653494834899902, 0.1140749454498291, 0.1141819953918457]
>
>i.e. the array protocol seems 5x slower than the fromstring() method.
>
>  
>
If you are going to be copying the data anyway, then there may be no 
advantage to the array protocol (in fact because it has to look up 
several attributes of the input object it can be slower).  When you use 
Numeric.array(na) it makes a copy of the data by default.

The idea is to be able to use the array protocol to not have to make 
copies of the data.

Try using  num = Numeric.array(na,copy=0)  in your first timing runs and 
see what that provides.

>Conversely, for Numeric --> numarray:
>
>  
>
>>>>t3=timeit.Timer("na=numarray.array(num)", "import numarray; import
>>>>        
>>>>
>Numeric;num=Numeric.arange(10)")
>  
>
>>>>t3.repeat(3,10000)
>>>>        
>>>>
>[1.3475611209869385, 1.3277668952941895, 1.3417830467224121]
>  
>
>>>>t4=timeit.Timer("na=numarray.array(buffer(num),type=num.typecode(),shape=num.shape)", "import numarray; import Numeric; num=Numeric.arange(10)")
>>>>t4.repeat(3,10000)
>>>>        
>>>>
>[0.42027187347412109, 0.41690587997436523, 0.41626906394958496]
>
>in this case, the array protocol is 3x slower than using the buffer
>interface.
>  
>
Again, you are making copies of the data.  I'm not sure how numarray 
handles the array protocol on consumption of the interface, so I can't 
comment further. 

-Travis





More information about the Numpy-discussion mailing list