Why is this loop in Cython as slow as its Python equivalent?

Viewed 315

The following code rearranges bits in an RGBA4 texture data array.

cpdef bytes toDDSrgba4(bytearray data):
    cdef bytes new_data = b''
    cdef u32 i, pixel, new_pixel
    cdef u32 red, green, blue, alpha

    for i in range(0, len(data), 2):
        pixel = int.from_bytes(data[i:i+2], "big")

        red = (pixel >> 12) & 0xF
        green = (pixel >> 8) & 0xF
        blue = (pixel >> 4) & 0xF
        alpha = pixel & 0xF

        new_pixel = (red << 8) | (green << 4) | blue | (alpha << 12)
        new_data += (new_pixel).to_bytes(2, "big")

    return new_data

However, even with correct variable typing, it is just as slow as its Python equivalent.
What is the cause?

2 Answers
Related