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?

有帮助吗?

解决方案

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.

其他提示

A few options:

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

  • use another serializer like msgpack

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top