How Much Faster is StringBuilder respect of concat operator + in C# .Net -


this question has answer here:

how faster stringbuilder respect of concat operator + in c# .net how work @ low level work faster concat?

the microsoft developer network has great comparison:

although stringbuilder , string both represent sequences of characters, implemented differently. string immutable type. is, each operation appears modify string object creates new string.

for example, call string.concat method in following c# example appears change value of string variable named value. in fact, concat method returns value object has different value , address value object passed method. note example must compiled using /unsafe compiler option. routines perform extensive string manipulation (such apps modify string numerous times in loop), modifying string repeatedly can exact significant performance penalty. alternative use stringbuilder, mutable string class. mutability means once instance of class has been created, can modified appending, removing, replacing, or inserting characters. stringbuilder object maintains buffer accommodate expansions string. new data appended buffer if room available; otherwise, new, larger buffer allocated, data original buffer copied new buffer, , new data appended new buffer.

the recommended usage string is:

  • when number of changes application make string quite small. in these instances stringbuilder may offer negligible or no performance improvement on string. (due way stringbuilder handles buffer)

  • when performing fix number of concatenation operations, particularly string literals. compiler should combine these 1 single operation.

  • when have perform extensive search operations while building string. stringbuilder lacks search methods such indexof or startswith. you'll have convert stringbuilder object string these operations may negate performance.

the recommended usage stringbuilder:

  • when expect application make unknown number of changes string @ design time (an example loops/random number strings).

  • when expect application make significant changes string.

so question shouldn't be, "which performs better", should "under circumstance 1 more ideal another?"


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -