What is the difference between performance between different types of string concatenation?

Asked

Viewed 196 times

6

I know 4 different types of concatenation of string in the c#:

// string Interpolation
$"valor {variavel} R$";

// Verbating string
@"texto qualquer
  pula a linha e continua o texto";

// concatenar 2 strings
"texto" + "texto";

// string.format
String.Format(variavel, " R$");

// Verbating string com string Interpolation
$@"UPDATE {variavel}
   SET campo = {variavel2}";

Until then I know what serves each of the commands, more I would like to know what impacts they have on memory and performance.

Ex: if I’m not mistaken the concatenation using the "+"

 "texto1" + "texto2"

He’d have two references in his memory "texto 1", "texto2" and when it concatenates it creates a third with the result "texto1 texto2".

How does it work in other cases? Or if what I explained this wrong (was more to make it easier to understand my doubt).

Because I use Resharper and he always recommends string Interpolation and the verbating string and I would like to know how far it facilitates reading and how far it disrupts performance and memory recycling.

An example of the code that Resharper suggests for me to use verbating string

var texto = "texto";

IDE mostrando o Resharper indicando

he suggests you stay:

var texto = @"texto"

IDE mostrando o Resharper indicando

He suggests moving to the Resource to string or use the verbating string, and also suggests creating a constant for the string that he points to.

IDE mostrando o Resharper indicando

As far as it goes and when it gets in the way?

  • Verbatim with interpolation? This one I didn’t know...

  • 1

    @JeffersonQuesado https://answall.com/search?q=verbatim+interpola%C3%A7%C3%A3o

  • @Maniero c# always being beautiful

  • @Maniero I added an example that happens with the suggestion of resharper with the prints, in the case when using a direct string as in the example he suggests this edition to interpolation string

  • 1

    He allows to do, it is not that he suggests, it may be that he wishes this among other things, he is not saying that it is better to do so.

1 answer

5


Congratulations, you’re above average :)

Only one of them is explicit concatenation, and none is required to concatenate even, some optimization could eliminate this, if possible in the case.

If these are just artificial examples, screwed up, because they may not happen as expected, or as in other scenarios, but you can talk more generally.

Do you swear Resharper suggests it that way? It doesn’t make sense to me, the way you posted it.

$"valor {variavel} R$";

This will be transformed to a string.Format() :) I wanted a simpler solution, at least for most cases, but it’s like this.

@"texto qualquer
pula a linha e continua o texto";

It turns into a single thing and is the same as having no more than one line, only a skip character will be included within the string, but for all intents and purposes it’s one thing.

"texto" + "texto";

This will probably be optimized and become a single string in memory. If the compiler cannot optimize a slightly different case it will be transformed into string.Concat(), there internally can be a simple concatenation or you can use a StringBuilder, or at least an optimization of it.

String.Format(variavel, " R$");

I think the syntax is wrong, but that’s beside the point.

See the source code (have to follow the links in it). He does something close to a StringBuilder riding the string. There have been optimizations to reduce memory allocations (actually at framework all today can be allocated much less than before, if you know what you are doing, and indeed allocation is something bad) and I think they will optimize more. If all goes well there will be no over-allocations, only one will solve.

$@"UPDATE {variavel}
  SET campo = {variavel2}";

It is the same already quoted, there will be only one string making a formatting, which will require a cost to enter the values.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.