InputStream vs. SocketChannel performance?

D

Daisy

I'm trying to improve the performance of some Java that someone else
wrote. This code uses SocketChannel's read() method. I'm considering
rewriting the code using InputStream (which I know is fast) to improve
performance. I'd appreciate the experience and opinions of others to
help me decide whether to rewrite.

Is SocketChannel's read() method much more time consuming than
InputStream's read()?
 
C

Chris Smith

Daisy said:
I'm trying to improve the performance of some Java that someone else
wrote. This code uses SocketChannel's read() method. I'm considering
rewriting the code using InputStream (which I know is fast) to improve
performance. I'd appreciate the experience and opinions of others to
help me decide whether to rewrite.

Is SocketChannel's read() method much more time consuming than
InputStream's read()?

There is unlikely to be any visible difference whatsoever in the read
operation. How you use the resulting data, though, could yield
performance differences. For example, if you user SocketChannel and
then immediately copy the results from a ByteBuffer into a byte[], then
that copy could be expensive for large amounts of data.

In the end, though, I'd expect that waiting on the socket itself would
swamp any performance issues you'd get in code, unless this is a socket
connected to localhost.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
T

Thomas Weidenfeller

Daisy said:
I'm trying to improve the performance of some Java that someone else
wrote. This code uses SocketChannel's read() method. I'm considering
rewriting the code using InputStream (which I know is fast) to improve
performance.

First things first. Did you run a profiler to confirm this is indeed the
bottleneck in the application? If not, do that first.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top