Joe Duffy's Blog implies using string.Substring
is more efficient than string.Split
.
Joe Duffy的博客暗示使用字符串。子字符串比string.Split更有效。
I don't know if its saying the Substring
method does not allocate a new string or if it is just more efficient because it does not make any unneeded allocations. Can you please explain how it is more efficient and show an example.
我不知道它是否表示子字符串方法不分配一个新字符串,或者它是否只是更有效,因为它没有做任何不必要的分配。你能解释一下它是如何更有效的吗?并举例说明。
I understand his first example as creating an array and then processing each of the strings in the array.
我理解他的第一个例子:创建一个数组,然后处理数组中的每个字符串。
string str = ...;
string[] substrs = str.Split(',');
foreach (string subtr in substrs) {
Process(substr);
}
How is the following more efficient
下面的方法如何更有效
string str = ...;
int lastIndex = 0;
int commaIndex;
while ((commaIndex = str.IndexOf(',', commaIndex)) != -1) {
Process(substr, lastIndex, commaIndex);
lastIndex = commaIndex + 1;
What I see is using String.IndexOf
to find the index of the comma then processing the string. I assume he intends to use String.Substring
to extract the data during his processing. One of the comments below suggested he may be pulling it character by character. Would he be pulling characters until he hits the next comma possibly building up an array of char?
我看到的是使用字符串。查找逗号的索引,然后处理字符串。我猜他打算用绳子。子字符串,以便在处理过程中提取数据。下面的一条评论暗示他可能在扮演一个又一个角色。他会一直拉字符直到碰到下一个逗号,可能会生成一个字符数组吗?
1 个解决方案
#1
7
Good grief.
好悲伤。
Old joke: The manager wanted to know if programmer A or programmer B was the better programmer, so he staged a contest. They both were to write a program to solve a given complicated problem, and the one who wrote the best program would win.
老笑话:经理想知道程序员A还是程序员B是更好的程序员,所以他举办了一场比赛。他们都要写一个程序来解决一个给定的复杂问题,而写得最好的程序的人会赢。
The two programmers submitted their answers. Programmer A's program ran fastest, and the manager was about to declare him to be the winner when programmer B pointed out that the answer provided by programmer A's program was a bit off.
两个程序员提交了他们的答案。程序员A的程序运行得最快,当程序员B指出程序员A的程序提供的答案有点偏差时,经理正要宣布他是赢家。
"But my program is still fastest, I deserve to win", said programmer A.
程序员A说:“但是我的程序还是最快的,我应该赢。”
"If the answer doesn't have to be correct, I can write a program that is 10 times faster than yours", retorted programmer B.
“如果答案不一定是正确的,我可以写一个比你快10倍的程序,”程序员B反驳道。
Joe Duffy's second example, where he avoids using string.Split(), is wrong. It won't compile. The variable "substr" is undefined.
Joe Duffy()的第二个例子中避免使用string.Split(),是错误的。它不会编译。变量“substr”没有定义。
I rest my case.
我休息。
#1
7
Good grief.
好悲伤。
Old joke: The manager wanted to know if programmer A or programmer B was the better programmer, so he staged a contest. They both were to write a program to solve a given complicated problem, and the one who wrote the best program would win.
老笑话:经理想知道程序员A还是程序员B是更好的程序员,所以他举办了一场比赛。他们都要写一个程序来解决一个给定的复杂问题,而写得最好的程序的人会赢。
The two programmers submitted their answers. Programmer A's program ran fastest, and the manager was about to declare him to be the winner when programmer B pointed out that the answer provided by programmer A's program was a bit off.
两个程序员提交了他们的答案。程序员A的程序运行得最快,当程序员B指出程序员A的程序提供的答案有点偏差时,经理正要宣布他是赢家。
"But my program is still fastest, I deserve to win", said programmer A.
程序员A说:“但是我的程序还是最快的,我应该赢。”
"If the answer doesn't have to be correct, I can write a program that is 10 times faster than yours", retorted programmer B.
“如果答案不一定是正确的,我可以写一个比你快10倍的程序,”程序员B反驳道。
Joe Duffy's second example, where he avoids using string.Split(), is wrong. It won't compile. The variable "substr" is undefined.
Joe Duffy()的第二个例子中避免使用string.Split(),是错误的。它不会编译。变量“substr”没有定义。
I rest my case.
我休息。