On 6/6/11 2:09 PM, Fritz Richter wrote:
Am 06.06.2011 um 14:02 schrieb Emmanuel Lecharny:
On 6/6/11 1:42 PM, Fritz Richter wrote:
Hi together,
in my current project I have the following szenario:
I have to continuously stream data via UDP in chunks which are about 1200
bytes. I only want to send this data to one specific host,
so this is the only connection which I need.
My data is prepared natively and it is stored in a ByteBuffer, which I
prepare in the background and reuse via an object pool.
My first approach was to send it via the old plain java way like this
example here:
byte[] data = new byte[byteBuffer.capacity()];
byteBuffer.rewind();
byteBuffer.get(data);
DatagramPacket udp = new DatagramPacket(data, 0, data.length,
holder.getRemoteAddress());
serverSocket.send(udp);
I switched to Apache Mina as I thought it might be faster, the code now
looks like this:
byteBuffer.rewind();
IoBuffer ioBuffer = IoBuffer.wrap(byteBuffer);
IoSession session = getSession();
session.write(ioBuffer);
I figured out, that this solution is not really faster. Actually, I have
the
strong feeling, that it is actually a bit slower, maybe the GC is running
more often.
My question to you guys is, does it makes sense to use the NIO approach
here
or is just fine to use the old java approach?
Is it worth to use MINA and the IoBuffer directly or is the overhead of
the
framework to much?
Hi,
short answer : NIO will be 30% slower than plain BIO.
Now, having said that does not help to understand when you should use NIO
or BIO. basically the BIO approach means 1 connection => one thread when
NIO can use a thread pool to deal with the incoming connections. Bottom
line, if you have to deal with many many connections, then NIO might be a
good solution. (many => tens of thousand).
One more thing : you should first focus on making your code working,
instead of focusing on performance, unless absolutely necessary.
Hi,
thanks a lot for this short and good answer. Than I stay with the BIO
approach.
Actually, our code is working and we really need to improve performance,
cause it runs on mobile devices and we need to save CPU as much as
possible.
Makes perfect sense.
No need to be smart to write good software, but being pragmatic is an
absolute must :)
--
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com