jzmq
jzmq copied to clipboard
Jzmq for String transfer
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 -
- Sender - ZMsg.addString(text) (underlying byte array representation cloned)
- Reciever - new String(ZFrame.getData()) (source bytes decoded and copied)
- 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
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");