I have a list of say 100k floats and I want to convert it into a bytes buffer.
buf = bytes()
for val in floatList:buf += struct.pack('f', val)
return buf
This is quite slow. How can I make it faster using only standard Python 3.x libraries.
I have a list of say 100k floats and I want to convert it into a bytes buffer.
buf = bytes()
for val in floatList:buf += struct.pack('f', val)
return buf
This is quite slow. How can I make it faster using only standard Python 3.x libraries.
Just tell struct
how many float
s you have. 100k floats takes about a 1/100th of a second on my slow laptop.
import random
import structfloatlist = [random.random() for _ in range(10**5)]
buf = struct.pack('%sf' % len(floatlist), *floatlist)