I was just trying to generate an XPath, for all the nodes in an XML file. First, i just used the string variable to append the Xpath's and i was fortunate to get the XPath's for all the Elements in the file.
Later, i thought, what am i doing..? I should instead use a StringBuilder, as it would give me a better throughput compared to the normal string variable. Atleast, this is what the MSDN documentation says..?
To my surprise, the latter approach was a disaster. It choked my system!!
Then, i thought, i should dive into the details and get to know myself what's what..?
Just have a look at the following code snippet..!
string temp2 = string.Empty;
for (int i = 0; i < 30; ++i)
string temp1 = temp2 + i.ToString();
temp2 += temp1;
This code is not doing much..and at the first glance we might think, it should just be fine..and we should be able to see the console with all the ouputs.
But what should i say, just give it a try..! It Chokes the System!!!
And so does this code snippet:
StringBuilder sb1 = new StringBuilder();
string temp1 = string.Empty;
temp1 = sb1.ToString() + i.ToString();
I would appreciate if some one can still explain to me how StringBuilder is much better than String.
Here are some snapshots that i managed to capture when my system was literally choking!!
Fig.1 & 2: Snapshots for String.
Fig.3 & 4: Snapshots for StringBuilder.