根据对 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/