469,924 Members | 1,387 Online

# Optimizing numpy

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

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

Thank You.

May 12 '07 #1
1 1450
On May 12, 10:52 pm, Gerdus van Zyl <gerdusvan...@gmail.comwrote:
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
May 12 '07 #2

### This discussion thread is closed

Replies have been disabled for this discussion.