python - Why is this loop so slow in Cython? -


this code rearranges bits in 534x713 rgba4 texture.

cpdef bytes toddsrgba4(bytearray data):     cdef bytes new_data = b''      cdef int pixel     cdef int red     cdef int green     cdef int blue     cdef int alpha     cdef int new_pixel     cdef int      in range(len(data) // 2):         pixel = int.from_bytes(data[2*i:2*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 

it's fast it's python equivalent, this:

def toddsrgba4(data):     new_data = b''      in range(len(data) // 2):         pixel = int.from_bytes(data[2*i:2*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 

both of them slow.

i have written very complex swizzle code isn't optimized , tested on texture, , it's still waaay faster this.

you're appending bytes object +=. that's slow, since has copy whole existing bytes object every time.

don't that. 1 better option use bytearray, , build bytes object out of bytearray @ end.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -