A scary realization
I wanted to find out how much faster using String.Format was, in comparison to just concatenating strings, using "&" ("+" in C#). To do so, I iterated a
String.Format
-statement and a equivalent concatenation statement 10'000'000 times, calculated the median and created a report. I did this a few times and found the following:
When combining three strings (e.g. "Hello " & strFirstName & "!")
String.Format
used 96% more memory, taking up 50% more processor time than an &-concatenation.
String.Concat
used 16% more memory, taking up virtually the same processor time (1 ms more) as an &-concatenation.
String.Format
used 10% less memory, taking up 59% more processor time than an &-concatenation.
String.Concat
used 2% less memory, taking up 3% less processor time than an &-concatenation.
-
When doing small concatenation, use "&" or "+".
When combining more than a few strings, use String.Concat
(When combining a large number of strings, use StringBuilder (see earlier posts))
Comments