String.Join vs. StringBuilder,哪个更快? [英] String.Join vs. StringBuilder, which is faster?

查看:85
本文介绍了String.Join vs. StringBuilder,哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,它接受一个I​​D列表(数百个)作为输入参数

,需要将数据作为逗号分隔的字符串传递给另一个步骤。

源可以用逗号分隔的字符串或

字符串数组轻松创建这个ID列表。我不希望它成为一个字符串,因为我想要超载这个函数,并且它的妹妹已经使用了一个字符串输入参数。现在

如果我定义了一个字符串数组中的函数,它解决了我的重载问题
问题,但是我必须将函数内部的数组转换为逗号

使用string.Join()分隔字符串。或者,我可以将输入

参数定义为StringBuilder(它只包含逗号分隔的

字符串),然后执行sb.ToString()来获取字符串。这将是一个更好的解决方案,使用字符串数组然后加入vs. StringBuilder

和ToString?


我知道如果我根本不会超负荷,从性能上来说它会有最好的理解,但是代码的可读性和维护变得更难了,因为这两个函数真正做到了非常相似的东西。


谢谢

Bob

I have a function that takes in a list of IDs (hundreds) as input parameter
and needs to pass the data to another step as a comma delimited string. The
source can easily create this list of IDs in a comma-delimited string or
string array. I don''t want it to be a string because I want to overload
this function, and it''s sister already uses a string input parameter. Now
if I define the function to take in a string array, it solves my overload
issue, but then I have to convert the array inside the function to a comma
delimited string using string.Join(). Alternatively, I can define the input
parameter as a StringBuilder (which just contains the comma delimited
string), and then do a sb.ToString() to get the string. Which would be a
better solution between using string array and then join vs. StringBuilder
and ToString?

I know if I don''t overload at all, it would make the best sense from a
performance perspective, but code readability and maintenance become harder
as the two functions really do the very similar things.

Thanks
Bob

推荐答案

因为字符串是不可变(每次分配一个新的内存地址

你改变字符串),StringBuilder是要走的路。

"鲍勃" <博******* @ yahoo.com>在消息中写道

新闻:OH ************** @ TK2MSFTNGP12.phx.gbl ...
Since strings are immutable (a new memory address is allocated each time
your alter the string), the StringBuilder is the way to go.
" Bob" <bo*******@yahoo.com> wrote in message
news:OH**************@TK2MSFTNGP12.phx.gbl...
我有一个功能将ID(数百)列表作为输入
参数,并需要将数据作为逗号分隔的字符串传递给另一个步骤。
源可以使用逗号分隔的字符串或
字符串数组轻松创建此ID列表。我不希望它成为一个字符串,因为我想重载这个函数,它的姐姐已经使用了一个字符串输入参数。现在如果我定义了一个字符串数组中的函数,它解决了我的重载问题,但是我必须将函数内部的数组转换成逗号分隔的字符串使用字符串。加入()。或者,我可以将
输入参数定义为StringBuilder(它只包含逗号分隔的字符串),然后执行sb.ToString()来获取字符串。这将是一个更好的解决方案,使用字符串数组然后加入vs. StringBuilder
和ToString?

我知道如果我根本不超载,它会使从性能角度来看最好的感觉,但是代码可读性和维护变得更加困难,因为这两个函数真的做了非常相似的事情。

谢谢
鲍勃
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in a comma-delimited string or
string array. I don''t want it to be a string because I want to overload
this function, and it''s sister already uses a string input parameter. Now
if I define the function to take in a string array, it solves my overload
issue, but then I have to convert the array inside the function to a comma
delimited string using string.Join(). Alternatively, I can define the input parameter as a StringBuilder (which just contains the comma delimited
string), and then do a sb.ToString() to get the string. Which would be a
better solution between using string array and then join vs. StringBuilder
and ToString?

I know if I don''t overload at all, it would make the best sense from a
performance perspective, but code readability and maintenance become harder as the two functions really do the very similar things.

Thanks
Bob



字符串不可变的事实与Bob的

问题有什么关系? String.Join是该类的静态方法,并在调用时创建一个新的
字符串对象。我个人会选择数组加入,它会更容易理解(你传递数组或值列表)

你赢了'不必循环将所有值放入字符串构建器。

这永远不会比调用Join更快。当然除非您的输入是

不是字符串数组(您说的是ID列表,不确定

类型)。


Jerry


" Scott M." < S - *** @ BADSPAMsnet.net>在消息中写道

news:Ob ************** @ TK2MSFTNGP12.phx.gbl ...
What does the fact that strings are immutable have to do with Bob''s
question? String.Join is a static method of that class and creates a new
string object when called. Personally I would go with an array and Join, it
will be easier to understand (you''re passing an array or a list of values)
and you won''t have to loop to put all your values into a string builder.
That will never be faster than calling Join. Unless of course your input is
not an array of strings (you''re saying a list of IDs, not sure about the
type there).

Jerry

"Scott M." <s-***@BADSPAMsnet.net> wrote in message
news:Ob**************@TK2MSFTNGP12.phx.gbl...
因为字符串是不可变的(每次你改变字符串时都会分配一个新的内存地址),StringBuilder就是你的选择。

"鲍勃" <博******* @ yahoo.com>在消息中写道
新闻:OH ************** @ TK2MSFTNGP12.phx.gbl ...
Since strings are immutable (a new memory address is allocated each time
your alter the string), the StringBuilder is the way to go.
" Bob" <bo*******@yahoo.com> wrote in message
news:OH**************@TK2MSFTNGP12.phx.gbl...
我有一个函数列入一个列表ID(数百)作为输入
I have a function that takes in a list of IDs (hundreds) as input


参数

并且需要将数据作为逗号分隔的字符串传递给另一个步骤。
and needs to pass the data to another step as a comma delimited string.


source可以在逗号分隔的字符串或
字符串数组中轻松创建此ID列表。我不希望它成为一个字符串,因为我想重载这个函数,它的姐姐已经使用了一个字符串输入参数。
现在,如果我定义了一个字符串数组中的函数,它解决了我的
重载问题,但是我必须使用字符串将函数内部的数组转换为
逗号分隔字符串。加入()。或者,我可以将
source can easily create this list of IDs in a comma-delimited string or
string array. I don''t want it to be a string because I want to overload
this function, and it''s sister already uses a string input parameter. Now if I define the function to take in a string array, it solves my overload issue, but then I have to convert the array inside the function to a comma delimited string using string.Join(). Alternatively, I can define the


输入

参数定义为StringBuilder(它只包含以逗号分隔的字符串),然后执行sb.ToString()得到字符串。哪个是
a更好的解决方案,使用字符串数组然后加入与
StringBuilder和ToString?

我知道如果我根本不超载,它会使从性能角度来看最好的感觉,但代码可读性和维护变得
parameter as a StringBuilder (which just contains the comma delimited
string), and then do a sb.ToString() to get the string. Which would be a better solution between using string array and then join vs. StringBuilder and ToString?

I know if I don''t overload at all, it would make the best sense from a
performance perspective, but code readability and maintenance become


更难

因为这两个函数真的做了非常相似的事情。

谢谢
Bob
as the two functions really do the very similar things.

Thanks
Bob




String.Join由运行内部实现 - 时间。它由

ConcatenateJoinHelperArray方法提供服务,结果非常快,

创建最终字符串而不是稍微慢一点

,因为String.Join将会相当快一些。


使用System;

使用System.Text;


公共类JoinVsBuilder {

private static string [] strings = new string [0];


private static void Main(string [] args){

int count = int .Parse(args [0]);


strings = new string [count];

for(int i = 0; i< count; i ++ ){

strings [i] = i.ToString();

}


DateTime start,end;


start = DateTime.Now;

string newStr = string.Join(" foo",strings);

end = DateTime。现在;

Console.WriteLine(" String :: Join timing is {0}",end-start);


StringBuilder sb = new StringBuilder ();

start = DateTime.Now;


//比担心什么时候更快追加连接器

sb.Append(strings [0]);

for(int i = 1;我< strings.Length; i ++){

sb.Append(" foo");

sb.Append(strings [i]);

}

string newStr2 = sb.ToString();

end = DateTime.Now;

Console.WriteLine(" StringBuilder :: Append timing is { 0}",end-start);

}

}


C:\Projects\CSharp \ Samples \ JoinVsBuilder> JoinVsBui lder.exe 1000000

字符串::加入时间为00:00:00.4606624

StringBuilder ::追加时间为00:00:02.9141904

-

贾斯汀罗杰斯

DigiTec Web Consultants,LLC。

博客: http://weblogs.asp.net/justin_rogers


" Jerry III" < JE ****** @ hotmail.com>在消息中写道

新闻:uT **************** @ TK2MSFTNGP09.phx.gbl ...
String.Join is implemented internally by the run-time. It is serviced by the
ConcatenateJoinHelperArray method which turns out to be extremely fast at
creating the final string as opposed to the slightly slower
StringBuilder.Append. If you have an array of strings, I would recommend using
that with String.Join over StringBuilder.Append since String.Join is going to be
quite a bit faster.

using System;
using System.Text;

public class JoinVsBuilder {
private static string[] strings = new string[0];

private static void Main(string[] args) {
int count = int.Parse(args[0]);

strings = new string[count];
for(int i = 0; i < count; i++) {
strings[i] = i.ToString();
}

DateTime start, end;

start = DateTime.Now;
string newStr = string.Join("foo", strings);
end = DateTime.Now;
Console.WriteLine("String::Join timing is {0}", end-start);

StringBuilder sb = new StringBuilder();
start = DateTime.Now;

// Faster than worrying about when to append the connector
sb.Append(strings[0]);
for(int i = 1; i < strings.Length; i++) {
sb.Append("foo");
sb.Append(strings[i]);
}
string newStr2 = sb.ToString();
end = DateTime.Now;
Console.WriteLine("StringBuilder::Append timing is {0}", end-start);
}
}

C:\Projects\CSharp\Samples\JoinVsBuilder>JoinVsBui lder.exe 1000000
String::Join timing is 00:00:00.4606624
StringBuilder::Append timing is 00:00:02.9141904
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Jerry III" <je******@hotmail.com> wrote in message
news:uT****************@TK2MSFTNGP09.phx.gbl...
什么是字符串不可变的事实与鲍勃的问题有关吗? String.Join是该类的静态方法,并在调用时创建一个新的
字符串对象。我个人会使用数组和Join,它会更容易理解(你传递一个数组或一个值列表)
并且你不必循环放入所有将您的值放入字符串构建器中。
这永远不会比调用Join更快。除非您的输入当然不是一个字符串数组(您说的是ID列表,不确定那里的
类型)。

Jerry
Scott M. < S - *** @ BADSPAMsnet.net>在消息中写道
新闻:Ob ************** @ TK2MSFTNGP12.phx.gbl ...
What does the fact that strings are immutable have to do with Bob''s
question? String.Join is a static method of that class and creates a new
string object when called. Personally I would go with an array and Join, it
will be easier to understand (you''re passing an array or a list of values)
and you won''t have to loop to put all your values into a string builder.
That will never be faster than calling Join. Unless of course your input is
not an array of strings (you''re saying a list of IDs, not sure about the
type there).

Jerry

"Scott M." <s-***@BADSPAMsnet.net> wrote in message
news:Ob**************@TK2MSFTNGP12.phx.gbl...
因为字符串是不可变的(新的内存地址)每次分配
你的改变字符串),StringBuilder是要走的路。

"鲍勃" <博******* @ yahoo.com>在消息中写道
新闻:OH ************** @ TK2MSFTNGP12.phx.gbl ...
Since strings are immutable (a new memory address is allocated each time
your alter the string), the StringBuilder is the way to go.
" Bob" <bo*******@yahoo.com> wrote in message
news:OH**************@TK2MSFTNGP12.phx.gbl...
我有一个函数列入一个列表ID(数百)作为输入
I have a function that takes in a list of IDs (hundreds) as input


参数

并且需要将数据作为逗号分隔的字符串传递给另一个步骤。
and needs to pass the data to another step as a comma delimited string.


source可以在逗号分隔的字符串或
字符串数组中轻松创建此ID列表。我不希望它成为一个字符串,因为我想重载这个函数,它的姐姐已经使用了一个字符串输入参数。现在,如果我定义函数以接受字符串数组,它解决了我的重载问题,但后来我必须使用string.Join()将函数内的数组转换为逗号分隔的字符串。或者,我可以将
source can easily create this list of IDs in a comma-delimited string or
string array. I don''t want it to be a string because I want to overload
this function, and it''s sister already uses a string input parameter. Now if I define the function to take in a string array, it solves my overload issue, but then I have to convert the array inside the function to a comma delimited string using string.Join(). Alternatively, I can define the


输入

参数定义为StringBuilder(它只包含以逗号分隔的字符串),然后执行sb.ToString()得到字符串。哪个是使用字符串数组然后连接与StringBuilder和ToString之间的更好的解决方案?

我知道如果我根本不过载,它将从
性能视角,但代码可读性和维护成为
parameter as a StringBuilder (which just contains the comma delimited
string), and then do a sb.ToString() to get the string. Which would be a better solution between using string array and then join vs. StringBuilder and ToString?

I know if I don''t overload at all, it would make the best sense from a
performance perspective, but code readability and maintenance become


更难

因为这两个函数确实做了非常相似的事情。

谢谢 Bob
as the two functions really do the very similar things.

Thanks
Bob





这篇关于String.Join vs. StringBuilder,哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆