Optimizing numpy

sturlamolden sturlamolden at yahoo.no
Sat May 12 19:11:16 EDT 2007


On May 12, 10:52 pm, Gerdus van Zyl <gerdusvan... at gmail.com> wrote:
> I have the following, that is used to convert pixel data and thus
> should be as fast as possible:
>
> b = numpy.ndarray (shape=(w,h,4), dtype=numpy.uint8)
>
> a = numpy.frombuffer(buf, numpy.uint8)
> a.shape = (w, h, 3)
>
> b[:,:,0] = a[:,:,2]
> b[:,:,1] = a[:,:,1]
> b[:,:,2] = a[:,:,0]
> b[:,:,3] = 255

You can express this as:

b[:,:,0:3] = a[:,:,2:-1:-1]
b[:,:,3] = 255


> Can anyone tell me if there is a faster way? Will making use of
> weave.blitz or pyrex help?


If you are going to use wave, then don't bother with weave.blitz use
wave.inline instead. You'll need something like this:

code = """
   register char a0, a1, a2;
   for (int i=0; i<w*h; i++)  {
        a0 = *a++;
        a1 = *a++;
        a2 = *a++;
        *b++ = a2;
        *b++ = a1;
        *b++ = a0;
        *b++ = 0xFF;
   }
"""
wave.inline(code,['a','b','h','w'],compiler='gcc')


Sturla Molden





More information about the Python-list mailing list