Frage

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?

War es hilfreich?

Lösung

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.

Andere Tipps

A few options:

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

  • use another serializer like msgpack

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top