String.Format performance

Since I work with a lot of data-centric applications, I've come to really like String.Format. There's something nice about seeing all your single quotes without them being obscured by double quotes.

For example, if I were adding a call to javascript function as an attribute of a control for use in client-side validation of an aspx page, I can use

chk.Attributes.Add("onclick", String.Format("updateSelectedStructure({0},{1}.checked);", workTicketId, chk.ClientID))

instead of

chk.Attributes.Add("onclick", "updateSelectedStructure(" & workTicketId & "," & chk.ClientID & ".checked);")

Personally, I find the first easier to comprehend. Of course, this led to a discussion with a colleague on the pros and cons of this approach. One question that came up was performance... which approach performs better, and by how much.

So I instrumented a simple test case, similiar to the example above. It turns out that string concatenation is roughly an order of magnitude faster than calling String.Format. One of these days, I'll get around to comparing it to StringBuilder...

Print | posted on Wednesday, May 16, 2007 1:00 PM

Feedback

No comments posted yet.

Your comment:





 
Please add 7 and 7 and type the answer here:

Copyright © Jeff Certain

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski