Members - Reputation: 111
Posted 03 December 2012 - 09:14 PM
Right now Im using string += array[count] + '\n'. It works, but it takes pretty long time to do it. It pretty much about two minutes of the program looking like its crashed. So if you guys can help, thanks.
Members - Reputation: 1367
Posted 03 December 2012 - 11:23 PM
Crossbones+ - Reputation: 2466
Posted 04 December 2012 - 01:57 AM
I had a similar thing once with CComBSTRs which went from 2 minutes to about 6 seconds just by building up the various substrings from certain sections separately from concatenating that onto the main string, which was a small change. Still not the most efficient, but it more than met the performance requirements, and we spent more time on it in another iteration.
My website dedicated to sorting algorithms
Members - Reputation: 523
Posted 04 December 2012 - 03:29 PM
Consider - all strings in .net languages are immutable - they cannot be changed once created. So;
A = A + B then
A = A + C then
A = A + D
essentially means creating a new string called A and filling it with A + C. Repeat 10,000 times and you have the memory heap fragmentation from hell.
StringBuilder keeps a list of the pointers to the original strings, calculates the size, and copies them all in one operation, meaning the intermediate instances of A shown above are not created. Hugely faster and more memory efficient.
Members - Reputation: 1492
Posted 04 December 2012 - 03:39 PM
[source lang="csharp"] //this just generates a "big string array" to play with List<string> bigList = new List<string>(); for (int i = 0; i < 25000; ++i) bigList.Add("someMediumLengthString"); string bigStringArray = bigList.ToArray(); //measure time DateTime start = DateTime.Now; //now we do the part that matters string ourHugeCombinedString = string.Join<string>('\n'.ToString(), bigStringArray); //print out the time it took to make the giant string TimeSpan timeToMakeCombinedString = DateTime.Now.Subtract(start); System.Diagnostics.Debug.WriteLine(timeToMakeCombinedString.TotalMilliseconds.ToString()); //prints out "2,0001" on my system, so 2 ms![/source]
Crossbones+ - Reputation: 10530
Posted 04 December 2012 - 05:26 PM
string combinedString = string.Join("\n", array);
It should be noted that the latest version of .Net includes both a string.Join which takes an array AND one which works on IEnumerable<T>.
Edited by Nypyren, 04 December 2012 - 05:27 PM.