What is the Complexity of StringBuilder.ToString()

ⅰ亾dé卋堺 提交于 2019-12-25 03:55:41

问题


in C#, What is the Complexity of StringBuilder.ToString()? Is it O(1), O(N), or something else?


回答1:


It varies between framework version; in older versions StringBuilder works on a string directly, so there is no additional cost in .ToString(): it just hands you the data directly (which can mean oversized, but it makes it work); so O(1).

In newer framework version, it uses a char[] backing buffer, so now when you .ToString() it will probably need to copy 2 x Length bytes, making it O(N).



来源:https://stackoverflow.com/questions/24250487/what-is-the-complexity-of-stringbuilder-tostring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!