StringBuilder的与字符串连接 [英] stringbuilder versus string concat
问题描述
在我的项目,我在整个循环数据视图的结果。
In my project I am looping across a dataview result.
string html =string.empty;
DataView dV = data.DefaultView;
for(int i=0;i< dV.Count;i++)
{
DataRowView rv = dV[i];
html += rv.Row["X"].Tostring();
}
以DV的行数将永久被3或4。
Number of rows in dV will alway be 3 or 4.
是更好地使用字符串连接+ = opearator或StringBuilder的这种情况下,为什么?
Is it better to use the string concat += opearator or StringBuilder for this case and why?
推荐答案
我会使用的StringBuilder
在这里,只是因为它描述你在做什么。
I would use StringBuilder
here, just because it describes what you're doing.
有关的3或4串的简单拼接,它可能不会作出任何显著差异,字符串连接的可能的连稍微快一点 - 但如果你错了,还有的地段的行,的StringBuilder
将开始变得更加高效,而且它的总是的更具描述性的,你在做什么。
For a simple concatenation of 3 or 4 strings, it probably won't make any significant difference, and string concatenation may even be slightly faster - but if you're wrong and there are lots of rows, StringBuilder
will start getting much more efficient, and it's always more descriptive of what you're doing.
另外,使用这样的:
string html = string.Join("", dv.Cast<DataRowView>()
.Select(rv => rv.Row["X"]));
请注意,你没有任何的排序此刻的字符串之间的分隔符的。你确定这就是你想要什么? (另请注意,您的code没有在此刻做出了很大的意义 - ?你不使用 I
在循环为什么)
Note that you don't have any sort of separator between the strings at the moment. Are you sure that's what you want? (Also note that your code doesn't make a lot of sense at the moment - you're not using i
in the loop. Why?)
我对字符串连接的文章肚里的更多细节的为什么它使用的StringBuilder
时的价值。
I have an article about string concatenation which goes into more detail about why it's worth using StringBuilder
and when.
编辑:对于那些谁怀疑字符串连接可以更快,这里是一个考验 - 与故意肮脏的数据,但只是为了证明这是可能的:
For those who doubt that string concatenation can be faster, here's a test - with deliberately "nasty" data, but just to prove it's possible:
using System;
using System.Diagnostics;
using System.Text;
class Test
{
static readonly string[] Bits = {
"small string",
"string which is a bit longer",
"stirng which is longer again to force yet another copy with any luck"
};
static readonly int ExpectedLength = string.Join("", Bits).Length;
static void Main()
{
Time(StringBuilderTest);
Time(ConcatenateTest);
}
static void Time(Action action)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
// Make sure it's JITted
action();
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < 10000000; i++)
{
action();
}
sw.Stop();
Console.WriteLine("{0}: {1} millis", action.Method.Name,
(long) sw.Elapsed.TotalMilliseconds);
}
static void ConcatenateTest()
{
string x = "";
foreach (string bit in Bits)
{
x += bit;
}
// Force a validation to prevent dodgy optimizations
if (x.Length != ExpectedLength)
{
throw new Exception("Eek!");
}
}
static void StringBuilderTest()
{
StringBuilder builder = new StringBuilder();
foreach (string bit in Bits)
{
builder.Append(bit);
}
string x = builder.ToString();
// Force a validation to prevent dodgy optimizations
if (x.Length != ExpectedLength)
{
throw new Exception("Eek!");
}
}
}
在我的机器上的结果(编译 / O + /调试 -
)
StringBuilderTest: 2245 millis
ConcatenateTest: 989 millis
我已经运行了好几次,包括扭转测试的顺序,结果是一致的。
I've run this several times, including reversing the order of the tests, and the results are consistent.
这篇关于StringBuilder的与字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!