开发者

StringBuilder, differences between two syntaxes

开发者 https://www.devze.com 2023-03-11 13:27 出处:网络
Is there a difference between : StringBuilder requete = new StringBuilder(); requete.Append(\" INSERT into \" + listeLibelleTable[index] + \" ( \");

Is there a difference between :

StringBuilder requete = new StringBuilder();
requete.Append(" INSERT into " + listeLibelleTable[index] + " ( ");

and

StringBuilder requete = new StringBuilder();
requete.Append(" INSERT into ");
requete.Append(listeLibelleTable[index]);
requete.Append(" ( ");

When I say "difference" I mean in terms of performance, if this code is in a loop for example.

I think these line

requete.Append(" INSERT into " + listeLibelleTable[index] + " ( ");

is resolved at compile time so It should not be any impact in terms of perf开发者_Go百科ormance but I'm not sure of that


Unless listeLibelleTable[index] can indeed be resolved at compile time (which I greatly doubt), using the string concatenation seems to be counter productive to the use of the StringBuilder.

In your second example you are concatenating a string and then appending it instead of appending to the StringBuilder.

In either case, you should probably use AppendFormat for readability:

requete.AppendFormat(" INSERT into {0} ( ", listeLibelleTable[index]);


Your first code is performing the concatenation, building a complete string and then appending it to the StringBuilder. Assuming you're also going to append other things, the second form could be a little bit faster... it doesn't need the temporary string with the result of that part of the concatenation.

On the other hand, if you're performing a database operation, the difference is going to be immeasurably small.


I think doing this:

" INSERT into " + listeLibelleTable[index] + " ( "

you use more memory to store result of concat operation!


String concatenation will create new String object and copy exisiting value, and despose old string. show it will be slow. check http://kaioa.com/node/59 for reference

0

精彩评论

暂无评论...
验证码 换一张
取 消