当前位置:首页 > 开发 > 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#解析Torrent获取磁力链

NuGet添加 MonoTorrentusing MonoTorrent;string&n...

C#一些重写

        pr...

在 C# 中实现类似于 Windows 资源管理器的“名称”排序方式

要在 C# 中实现类似于 Windows 资源管理器的“名称”排序方式,你需要考虑以下几点:1. 不...

C# 跳出foreach循环

在 C# 中,如果你想在 foreach 循环内部提前跳出当前这一轮循环,继续执行下一轮循环,可以使...

C# Winform 拖放文件

private void Form1_Load(object send...

C# decimal

概述在 C# 中,decimal是一种数据类型,用于表示高精度的十进制数值。它主要用于需要精确计算的...