[Numpy-discussion] Performance of the array protocol

Todd Miller jmiller at stsci.edu
Tue Nov 1 09:28:25 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.
>
>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.
>
>I'm wondering whether this relatively poor performance in present
>implementation of the array protocol is surmountable or is an intrinsic
>limitation of it.
>
>Thanks,
>  
>
I was working on improving this for numarray yesterday so some 
improvements are in CVS now.

I moved some of the Python array interface properties down to C 
attributes and the performance ratio for my debug Python is now <2x for 
numarray-->Numeric.   numarray's array interface for Numeric-->numarray 
was degenerating to fromlist();   I added array interface "consumption" 
support for numerical arrays by beefing up numarray's array() function.

I tweaked your benchmarks a little to support profiling as well as 
timing and attached the result. 

% python arrayif_bench.py

numarray-->Numeric array_if: [0.35534501075744629, 0.36865997314453125, 0.36826896667480469]

numarray-->Numeric fromstring: [0.36841487884521484, 0.21085405349731445, 0.20747494697570801]

Numeric-->numarray array_if: [0.73384881019592285, 0.6396629810333252, 0.60234308242797852]

Numeric-->numarray buffer_if: [0.36455512046813965, 0.24601507186889648, 0.23759102821350098]



-------------- next part --------------
A non-text attachment was scrubbed...
Name: arrayif_bench.py
Type: application/x-python
Size: 1102 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20051101/253779e3/attachment.bin 


More information about the Numpy-discussion mailing list