当前位置:首页 > 开发 > 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# NPOI

使用NPOI操作.xlsx以及其他格式的文档,并且无需安装OFFICE。实测无法加载带有密码的.xl...

C# XML

创建    XmlDocument xmlDoc ...

C# Browsable(bool)

在编程中(比如常见的 C# 语言在开发 Windows Forms 等应用程序时),Browsabl...

C# ref 和out

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

C# MemoryStream转为Image

        //...

C# i++和++i的区别

核心区别操作顺序            ...