Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

A few options:

  • use python's struct.pack / struct.unpack methods e.g. struct.pack("!L", 1234567)

  • use another serializer like msgpack

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top