Reputation: 16796
I want to send lots of numbers via zeromq but converting them to str is inefficient. What is the best way to send numbers via zmq?
Upvotes: 3
Views: 3523
Reputation: 19347
You state that converting numbers to str
is inefficient. And yet, unless you have a truly exotic network, that is exactly what must occur no matter what solution is chosen, because all networks in wide use today are byte-based.
Of course, some ways of converting numbers to byte-strings are faster than others. Performing the conversion in C code will likely be faster than in Python code, but consider also whether it is acceptable to exclude "long" (bignum) integers. If excluding them is not acceptable, the str
function may be as good as it gets.
The struct
and cpickle
modules may perform better than str
if excluding long integers is acceptable.
Upvotes: 4
Reputation: 8066
A few options:
use python's struct.pack / struct.unpack methods e.g. struct.pack("!L", 1234567)
use another serializer like msgpack
Upvotes: 5