SocketInputStream.read() takes 70% of the time

Hey all. I have a multithreaded program that reads some data over the net, and I have been profiling it with hprof to get the performance up.

Basically, my data format is an ASCII number followed by a newline, then that many bytes of data following. The data itself is XML.

my input function is essentially broken into 3 parts:

read 1 byte at a time until i hit a newline.

read x bytes into a buffer

convert the buffer into an XML object.

Profiling tells me that out of 42K samples, 30K are in the first part, 6K are in the second, and 4K are in the third. The 30K all come from SocketInputStream.read(). This doesn't seem right at all to me. I have included the first function below:

private String readUntil(char stop_char)

throws IOException

{

StringBuilder new_string =new StringBuilder();

while (true){

int in = input_stream.read();

if (in == -1 || in == stop_char){

break;

}

new_string.append((char)in);

}

return new_string.toString();

}

Any help that you can provide is greatly appreciated! Thanks!

[1662 byte] By [jgouldinga] at [2007-11-26 14:45:20]
# 1
That just means that it's blocked most of the time, which makes sense. It's not CPU usage.
ejpa at 2007-7-8 8:32:56 > top of Java-index,Archived Forums,Socket Programming...
# 2
As mentioned you have misinterpreted your profiling data.Also though is there a reason you are not using a BufferedStream? Because that will improve your performance.
cotton.ma at 2007-7-8 8:32:56 > top of Java-index,Archived Forums,Socket Programming...
# 3
[deleted]
ejpa at 2007-7-8 8:32:56 > top of Java-index,Archived Forums,Socket Programming...
# 4
> That just means that it's blocked most of the time,> which makes sense. It's not CPU usage.I kept going back and forth about whether cpu=samples was monitoring cpu or wallclock time. I guess this answers that. Would cpu=times produce cpu time then? Thanks!
jgouldinga at 2007-7-8 8:32:56 > top of Java-index,Archived Forums,Socket Programming...
# 5

> Also though is there a reason you are not using a

> BufferedStream? Because that will improve your

> performance.

Not really, I just didn't know it would provide that much of an increase. Considering that I am just reading small-ish XML nodes each time, could I expect a noticeable difference?

jgouldinga at 2007-7-8 8:32:56 > top of Java-index,Archived Forums,Socket Programming...
# 6
There's a big difference between 4096 system calls and one, so yes you would notice a difference.
ejpa at 2007-7-8 8:32:56 > top of Java-index,Archived Forums,Socket Programming...