Malekar Hardik

Hi,

I have a loop where in, i read the records from the dataset (about 9000 rows). I create comma sep. values using every row.
Now the issue is that after about 1700 odd rows, the string append operation gets really slow.
...
Any pointers as to what could be the issue....is the string type casusing this issue.

Excerpt:

string line = string.Empty;
int columnCount = data.Tables[0].Columns.Count;
int counter = 0;
foreach(DataRow row in data.Tables[0].Rows)
{
for(int i = 0; i < columnCount; ++i)
{
line += Convert.ToString(row.ItemArrayIdea) + ",";
}
line = line.Substring(0, line.Length - 1);
line += "\n";
counter++;
System.Console.Write("\b\b\b\b" + counter);
}



Re: Visual C# General String append operation slow.

Peter Ritchie

Yes, the operator+ of String is slow, use StringBuilder instead.






Re: Visual C# General String append operation slow.

Malekar Hardik

thanks man...really helped....now its damn fast.





Re: Visual C# General String append operation slow.

Peter Ritchie

operator+ is slow because it has to create a new string for each append. So, if you started out with "one", added "two", you'd then have 3 strings in memory: "one", "two", and "onetwo". Strings are interned so they can be reused, so every string you create is added to an interned string collection. e.g. if you started out with "one", then added another "one" to that you'd only end up with two strings in memory "one" and "oneone".

StringBuilder is designed to avoid creating these temporary strings and concentrate on the string as a block of chars until you call StringBuilder.ToString().






Re: Visual C# General String append operation slow.

Chad Moran

Peter is 100% correct whenever you modify a string you're creating a new instance of it in memory. The technical term for this is "immutable" meaning the object itself cannot be modified once created.






Re: Visual C# General String append operation slow.

Sean Fowler

I had a different situation a little while ago. I was only concatenating around half-a-dozen string values, but I was doing it thousands of times.

I tried StringBuilder, String.Format and good old +. Using + was significantly faster.

In your situation StringBuilder is clearly the right answer, but remember that for smaller concatenations + is fastest.

Actually now I think about it I'm not sure if I tried creating a static StringBuilder and re-using that; it's possible that may be faster still as you take away the instantiation cost of the StringBuilder.

Sean