DayLaborer
Programmer
I was asked to review and rewrite some code that takes a StreamReader and turns it into a StringBuilder. The original code was this:
When I saw that it was processing each character seperately, I thought I was making it far more efficient by changing it to this:
What I truly don't understand is that the time to update 1,000 records jumped from 17 seconds with the original code to over 2 minutes with my new, improved code!
Why?! Isn't the way I rewrote it better? Is there something I'm missing to make it faster?
Thanks,
Eliezer
Code:
for (int i = 1; i <= dataSize; i++)
{
if (streamReader.Peek() > 0)
{
streamReader.Read(c, 0, c.Length);
sbXML.Append(c);
}
}
Code:
while (!streamReader.EndOfStream)
{
sbXML.AppendLine(streamReader.ReadLine());
}
Why?! Isn't the way I rewrote it better? Is there something I'm missing to make it faster?
Thanks,
Eliezer