Possible Duplicate:
At what point does using a StringBuilder become insignificant or an overhead?
Related/Duplicate Questions
String vs StringBuilder
At what point does using a StringBuilder become insignificant or an overhead?
As plain as possible I have this method 1:
cmd2.CommandText = ("insert into " + TableName + " values (" + string.Join(",", insertvalues) + ");");
I am wondering if method 2 be faster if I would do:
StringBuilder sb2 = new StringBuilder();
sb2.Append("insert into ");
sb2.Append(TableName);
sb2.Append(" values (");
sb2.Append(string.Join(",", insertvalues));
sb2.Append(");");
cmd2.CommandText = sb2.ToString();
You could also try String.Format, which I believe uses a StringBuilder internally but has increased readability.
cmd2.CommandText = string.Format("insert into {0} values ({1});", TableName, string.Join(",", insertvalues));
(This is for C#)
For small programs this will be a premature optimization.
If you want to take into consideration these kinds of optimization then better measure it, because this depends on the size of the string concatenated also, apart from the number or appends.
Besides that IMO the StringBuilder method looks and reads better the StringBuilder does outperform string concatenation after 5 to 10 added strings according to http://dotnetperls.com/stringbuilder-performance
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With