# Inefficient usage of string.format?

I have some sample code where I used string.Format but after reading about benchmark performances by other people I believe that it may be better to do multiple appends by a StringBuilder

Here is the scenario:

In my gridView, I have a function called before a databind. The function looks like such:

public string getColumnText(String myParam)
{
StringBuilder sb = new StringBuilder();
return sb.Append(string.Format("<a href='javascript:Global.someFunction(\"{0}\");'>{1}</a>", myParam).ToString();
}


Is this a better alternative?

StringBuilder sb = new StringBuilder();
sb.Append("<a href = 'javascript:Global.someFunction(\"");
sb.Append(myParam"); etc...


Let's assume that this function will be called 10,000 times.

-
It doesn't directly matter how many times the function will be called. (And it's not even clear what exactly do you mean by that: Is that 10,000 times per year? Or per second? Or something completely else?) What matters is whether the first method is too slow for your performance requirements. –  svick Jan 7 '13 at 15:55
I would suggest running benchmarks on both and see what the results are. –  Gene S Jan 7 '13 at 16:17
@svick I don't see how its not clear. I mentioned that my function is going to be performed before gridview.databind() is called so implying that 10K cooresponds to the number of entries in my gridView –  Rhs Jan 7 '13 at 16:38
@Rhs Ok, but that wasn't the main point I was making. The most important thing is: is this actually too slow for you? If not, write what's most readable and don't worry about performance. –  svick Jan 7 '13 at 16:41

A StringBuilder is useful when you're constructing a string where a loop will be directly effecting it, i.e. if you were appending several strings together within a loop, I'd recommend using a StringBuilder. Here's an example of where you might use a StringBuilder.

StringBuilder sb = new StringBuilder()
for (int i = 0; i < someItems.length; i++)
{
sb.AppendFormat("{0} is item index {1}\n", someItems[i], i);
}
return sb.ToString();


There are some overheads with string.Format, however they aren't huge, especially not in your case. Unless you're concerned about micro-optimisations, I think in this scenario it all falls down to readability, and which you prefer.

An alternative method, which would be slightly faster than string.Format is to simply concatenate the string together, like so:

public string getColumnText(String myParam)
{
return "<a href='javascript:Global.someFunction(\"" + myParam + "\");'>" + myParam + "</a>";
}


You may also want to consider ensuring it's safe for your desired output, i.e. do you need to escape quotation marks for myParam, or does it need to be HTML encoded?

-
I disagree, string.Format is much easier for my eyes too parse. Having a concatenated mess of variables and text is hard to look at and a pain to manage down the road. –  Chuck Conway Jan 8 '13 at 21:11
I agree; I didn't imply using string concatenation with + was easier to read, I said it was slightly faster. I personally prefer string.Format for readability. –  Richard Jan 9 '13 at 9:25