0

What does StringBuilder initialization do behind the scenes in the code below? Also what is the time complexity for StringBuilder initialized with a string?

string str = "fubar";
StringBuilder sb = new StringBuilder(str);

I was wondering if it does an O(n) append behind the scenes, or does it in O(1) time.

  • https://referencesource.microsoft.com/#mscorlib/system/text/stringbuilder.cs,adf60ee46ebd299f – Dmitry Bychenko May 19 '17 at 15:05
  • Take a look! https://referencesource.microsoft.com/#mscorlib/system/text/stringbuilder.cs,adf60ee46ebd299f – itsme86 May 19 '17 at 15:06
  • StringBuilder got pretty convoluted at .NET 4.0, now using *ropes* for storage. Buffers linked together, intended to prevent the object from getting into the Large Object Heap. It is inevitably O(n) where n is the string length. Very small Oh, it runs at memory bus speed. The point of StringBuilder is that modifications to the string don't always require an O(n) allocation cost, like they do for the immutable System.String – Hans Passant May 19 '17 at 15:45

0 Answers0