当前位置:首页 > 开发 > C# > 正文内容

C# string与StringBuilder速度测试

C#3年前 (2022-10-30)
测试代码    
Stopwatch time1 = new Stopwatch();
Stopwatch time2 = new Stopwatch();

string str = String.Empty;
StringBuilder sb = new StringBuilder();



time1 .Start ();
for (int i = 0; i < 100000; i++)
{
    str+=i.ToString();
}
time1.Stop ();
Console.WriteLine(time1.Elapsed);




time2 .Start ();
for (int i = 0; i < 100000; i++)
{
    sb.Append(i.ToString());
}
time2.Stop();
Console.WriteLine(time2.Elapsed);



Console.ReadKey();


输出

00:00:13.2234835
00:00:00.0073885


转载请注明出处。

本文链接:http://www.pythonopen.com/?id=235

相关文章

C# ArrayList

添加的成员可以是任意类型    ArrayList arra...

C# 冒泡排序

int[] iage = { 11, 55,&nb...

C# Winform 拖放文件

private void Form1_Load(object send...

C# BackgroundWorker,在DoWork里更新控件内容

一般情况下不可以直接在BackgroundWorker的DoWork事件中更新 UI 控件在Back...

C# ref 和out

ref关键字概念:ref是 C# 中的一个关键字,用于按引用传递参数。当在方法调用中使用ref关键字...