NetworkStreams

  • Thread starter Thread starter Ron
  • Start date Start date
R

Ron

Hi all,

I created a POP3 client (with the help of some examples of the net:).
I'm using a StreamReader to read from the NetworkStream as follows:
while (sTmp != ".")
{
sMsg += sTmp + "\r\n";
sTmp = strmReader.ReadLine();
}

The problem is that this is really slow and uses a lot of CPU time.

Is there a faster way to do this??

Thanx,
Ron
 
Your string concatenation is going to kill you. You'd be better off using
a collection or array based storage for holding each individual string and
then joining them later. I do approximately the same operations for
newsgroups, downloading at over 1mbit per second while reconstructing
and decoding on a pretty lame machine. Never seen more than 10-15%
CPU utilization for these processes.
 
Ron said:
Hi all,

I created a POP3 client (with the help of some examples of the net:).
I'm using a StreamReader to read from the NetworkStream as follows:
while (sTmp != ".")
{
sMsg += sTmp + "\r\n";
sTmp = strmReader.ReadLine();
}

The problem is that this is really slow and uses a lot of CPU time.

Is there a faster way to do this??

Where is your slow down occuring? Is it in building sMsg or is it in the
call to strmReader.ReadLine()?

If its in building sMsg, you may want to look at System.Text.StringBuilder,
if its in strmReader.ReadLine(), you may get better performance by copying
large blocks into a MemoryStream and doing your parsing from there.
 
It was indeed the string concatenation. I am using the StringBuilder now,
and it has decreased the cpu usage dramatically.

Thanks.
Ron
 
Ron said:
It was indeed the string concatenation. I am using the StringBuilder now,
and it has decreased the cpu usage dramatically.

I suspected that it was, but it was possible that you were experiancing come
obscure bug caused by something with your socket settup, thats why I
suggested both, ;).

Depending on your purposes, Justin's suggestion may be better. If you are
decoding attachments or multipart mime messages it almost certainly is, but
if you are just sucking out plain text messages StringBuilder will work
fine.
 
Back
Top