[Numpy-discussion] Speed performance on array constant set

Travis Oliphant oliphant.travis at ieee.org
Fri Jan 20 08:42:02 EST 2006


Mark Heslep wrote:

> Im doing some work with the OpenCv* project.  Im using swig typemaps 
> to convert the Cv data structures to numarray which works well.  Id 
> like to restrict Cv use to what its strengths: complicated vision 
> processing algorithms like optical flow.  For the case of simple Cv 
> data manipulations, I'd rather use NumPy functions & methods but was 
> surprised at the performance comparison.
> - A simple scalar constant fill with cvSet.  'im' here is a wrapped Cv 
> image data structure.
>
>> python -m timeit -s "import opencv.cv as cv; im = 
>> cv.cvCreateImage(cv.cvSize(1000,1000), 8, 1)" "cv.cvSet( im, 
>> cv.cvRealScalar( 7 ) )"
>> 100 loops, best of 3: 2.58 msec per loop
>
>
> - If I try the equivalent with NumPy
>
>> python -m timeit -s "import numarray as na; a = na.zeros((1000,1000) 
>> )" "a[:,:] = 7"
>
>
>> 10 loops, best of 3: 45.1 msec per loop
>
>
> A >10x hit.   Am I using the preferred / optimal NumPy method here?  I 
> scanned the earlier Scalar  posts but thought that was boolean  type 
> only issue.
>

First of all,  try using NumPy instead of Numarray:    import numpy as na

Second:   use math (i.e.   a += 7)

Third:   There are specialized fill routines .fill()  in numpy and a 
simliar routine in Numarray that can be faster then indexing.

Best,

-Travis










More information about the NumPy-Discussion mailing list