c# - string.Split 方法的运行时间

标签 c# .net

根据对 this 的回复, 我很好奇 String.Split() 的运行时间是多少

string source = "source string;this,"; //length n
string[] splitted = source.Split(' ',',',';'); //delimiter array of length m

是 O(m*n) 吗?

最佳答案

根据 this线程,它是 O(N):

Internally, it uses a 2 pass routine. In the first pass, the index of the separator characters are discovered and stored. In the second pass, the string is "split" by calling Substring repeatedly and storing the results in an output array using the indices previously saved.

As such, the algorithm is effectively O(N), since it's doing linear passes through the input string.

注意:上面这句话的作者好像是SO user - 也许他可以提供更详细的答案。

如果您想自己查看源代码,请下载 ILSpy 等工具。 ,引用 mscorlib 并搜索 Split 实现。

关于c# - string.Split 方法的运行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9068020/

相关文章:

c# - 以最小的延迟快速显示非常高分辨率图像的缩略图

.net - 是一对一还是组件?休眠映射

.net - Dictionary 和 Hashtable 在线程安全方面的区别

c# - PictureBox 可以在 Windows 应用程序中显示动画 GIF 吗?

C# - 二进制搜索(排序)字典

c# - 组合 Func<Foo, object> 和 Action<object>

c# - 将带有参数的命令附加到 KeyUp 事件

c# - 将矩形添加到pdfpcell itextsharp中

c# - 通过 http 发送基本身份验证

c# - DataGridViewColumnHeader 加粗