jzmq icon indicating copy to clipboard operation
jzmq copied to clipboard

Jzmq for String transfer

Open computinglife opened this issue 8 years ago • 1 comments

Hi,

Iam currently using Jzmq without much issues. I do have an optimization query -

All the data I send across ZeroMQ are Strings, which follow this route -

  1. Sender - ZMsg.addString(text) (underlying byte array representation cloned)
  2. Reciever - new String(ZFrame.getData()) (source bytes decoded and copied)
  3. Later on, I append the text received to a StringBuilder.

I was wondering if this is the most efficient way to transmit data, since there seems to be a lot of copying of data involved and I was seeking to avoid all of it, to create efficiencies.

Are there any best practices here to achieve what i seek?

Thanks Arun

computinglife avatar Feb 13 '17 01:02 computinglife

Ditch ZMsg and ZFrame. Should increase your speed significantly (factor 10 in my expriments).

For me it took around 2ms for a request with ZMsg and ZFrame and only 0.2ms without them.

ZContext ctx = new ZContext();

Socket server = ctx.createSocket(ZMQ.REP);
server.bind("inproc://simple");

Socket client = ctx.createSocket(ZMQ.REQ);
client.connect("inproc://simple");


long start = System.nanoTime();
client.send("hello");

server.recvStr(Charset.defaultCharset());
long diff = System.nanoTime() - start;

System.out.println("took " + (double)diff/1000000 + "ms");

vs.

ZContext ctx = new ZContext();

ZMQ.Socket server = ctx.createSocket(ZMQ.REP);
server.bind("inproc://simple");

ZMQ.Socket client = ctx.createSocket(ZMQ.REQ);
client.connect("inproc://simple");

long start = System.nanoTime();

ZMsg msg = new ZMsg();
msg.addString("hello");
msg.send(client);

new ZFrame(server.recv());

long diff = System.nanoTime() - start;

System.out.println("took " + (double)diff/1000000 + "ms");

jcoeltjen avatar Mar 01 '17 14:03 jcoeltjen