在C#中StringBuilder的性能? [英] StringBuilder performance in C#?
问题描述
我在那里我加入一些字符串一个的StringBuilder
对象一样如下:
我想知道哪些一个是更好的方法在这里,第一个是这样的:
StringBuilder的SB =新的StringBuilder();
sb.Append(你好+如何+是+你);
和第二个是:
StringBuilder的SB =新的StringBuilder();
sb.Append(你好)追加(如何)追加(是)追加(您)。;
首先会更有效率。编译器将其转换为以下单呼:
StringBuilder的SB =新的StringBuilder();
sb.Append(HelloHowareyou);
性能测量
知道这是更快的最佳方式是,以测量它。我会开门见山地说:这里的结果(较小的时间意味着更快):
sb.Append(你好+如何+是+你):11.428s
sb.Append(你好)追加(如何)追加(是)追加(您) :15.314s
sb.Append(A + b + C + D):21.970s
sb.Append(一).Append(二).Append(三).Append(四):15.529s
给出的数目是执行操作100万次在紧凑循环的秒数。
结论
- 最快的是使用字符串和
+
。 - 但如果你有变量,使用
追加
更快比+
。第一个版本是慢,因为额外调用String.Concat
的
如果你要测试这个自己,这是我用来获取上述计时程序:
使用系统;
使用System.Text;
公共类节目
{
公共静态无效的主要()
{
日期时间起点,终点;
INT numberOfIterations =亿;
开始= DateTime.UtcNow;
的for(int i = 0; I< numberOfIterations ++ I)
{
StringBuilder的SB =新的StringBuilder();
sb.Append(你好+如何+是+你);
}
端= DateTime.UtcNow;
DisplayResult(sb.Append(\Hello\+ \How\+ \are\+ \you\),开始,结束);
开始= DateTime.UtcNow;
的for(int i = 0; I< numberOfIterations ++ I)
{
StringBuilder的SB =新的StringBuilder();
sb.Append(你好)追加(如何)追加(是)追加(您)。;
}
端= DateTime.UtcNow;
DisplayResult(sb.Append(\Hello\)。追加(\How\)。追加(\are\)。追加(\you\ ),开始,结束);
字符串=你好;
字符串B =如何;
串c =是;
串D =您;
开始= DateTime.UtcNow;
的for(int i = 0; I< numberOfIterations ++ I)
{
StringBuilder的SB =新的StringBuilder();
sb.Append(A + B + C + D);
}
端= DateTime.UtcNow;
DisplayResult(sb.Append(A + B + C + D),开始,结束);
开始= DateTime.UtcNow;
的for(int i = 0; I< numberOfIterations ++ I)
{
StringBuilder的SB =新的StringBuilder();
sb.Append(一).Append(二).Append(三).Append(四);
}
端= DateTime.UtcNow;
DisplayResult(sb.Append(一).Append(B).Append(C).Append(四),开始,结束);
到Console.ReadLine();
}
私有静态无效DisplayResult(字符串名称,日期时间的开始,结束日期时间)
{
Console.WriteLine({0,} -60:{1 ,6:0.000} S的名字,(结束 - 开始).TotalSeconds);
}
}
I have a StringBuilder
object where I am adding some strings like follows:
I want to know which one is better approach here, first one is this:
StringBuilder sb = new StringBuilder();
sb.Append("Hello" + "How" + "are" + "you");
and the second one is:
StringBuilder sb = new StringBuilder();
sb.Append("Hello").Append("How").Append("are").Append("you");
The first will be more efficient. The compiler will convert it to the following single call:
StringBuilder sb = new StringBuilder();
sb.Append("HelloHowareyou");
Measuring the performance
The best way to know which is faster is to measure it. I'll get straight to the point: here are the results (smaller times means faster):
sb.Append("Hello" + "How" + "are" + "you") : 11.428s
sb.Append("Hello").Append("How").Append("are").Append("you"): 15.314s
sb.Append(a + b + c + d) : 21.970s
sb.Append(a).Append(b).Append(c).Append(d) : 15.529s
The number given is the number of seconds to perform the operation 100 million times in a tight loop.
Conclusions
- The fastest is using string literals and
+
. - But if you have variables, using
Append
is faster than+
. The first version is slower because of an extra call toString.Concat
.
In case you want to test this yourself, here's the program I used to get the above timings:
using System;
using System.Text;
public class Program
{
public static void Main()
{
DateTime start, end;
int numberOfIterations = 100000000;
start = DateTime.UtcNow;
for (int i = 0; i < numberOfIterations; ++i)
{
StringBuilder sb = new StringBuilder();
sb.Append("Hello" + "How" + "are" + "you");
}
end = DateTime.UtcNow;
DisplayResult("sb.Append(\"Hello\" + \"How\" + \"are\" + \"you\")", start, end);
start = DateTime.UtcNow;
for (int i = 0; i < numberOfIterations; ++i)
{
StringBuilder sb = new StringBuilder();
sb.Append("Hello").Append("How").Append("are").Append("you");
}
end = DateTime.UtcNow;
DisplayResult("sb.Append(\"Hello\").Append(\"How\").Append(\"are\").Append(\"you\")", start, end);
string a = "Hello";
string b = "How";
string c = "are";
string d = "you";
start = DateTime.UtcNow;
for (int i = 0; i < numberOfIterations; ++i)
{
StringBuilder sb = new StringBuilder();
sb.Append(a + b + c + d);
}
end = DateTime.UtcNow;
DisplayResult("sb.Append(a + b + c + d)", start, end);
start = DateTime.UtcNow;
for (int i = 0; i < numberOfIterations; ++i)
{
StringBuilder sb = new StringBuilder();
sb.Append(a).Append(b).Append(c).Append(d);
}
end = DateTime.UtcNow;
DisplayResult("sb.Append(a).Append(b).Append(c).Append(d)", start, end);
Console.ReadLine();
}
private static void DisplayResult(string name, DateTime start, DateTime end)
{
Console.WriteLine("{0,-60}: {1,6:0.000}s", name, (end - start).TotalSeconds);
}
}
这篇关于在C#中StringBuilder的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!