This thread discusses options of serializing numpy arrays preserving their shape and dtype. While the most upvoted answer works, it is 5x slower than using np.tobytes
and np.from_buffer
. Is there a faster option?
import io
import numpy
x = np.ones((500,500,3), dtype=np.uint32)
def encode(x) -> str:
return x.tobytes()
def decode(s):
return np.frombuffer(s, dtype=np.uint32).reshape(500,500,3)
# Keeps dtype and shape!
def encode2(x: np.ndarray) -> str:
memfile = io.BytesIO()
np.save(memfile, x)
return memfile.getvalue()
def decode2(s: str) -> np.ndarray:
memfile = io.BytesIO()
memfile.write(s)
memfile.seek(0)
return np.load(memfile)
%%timeit
decode(encode(x)) # 0.193 ms per loop
# decode2(encode2(x)) # 1.09 ms per loop