当前位置:首页 > 开发 > 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://pythonopen.com/?id=235

相关文章

C# XML

创建    XmlDocument xmlDoc ...

C# 可空参数

using System; using System.Runtime.Inte...

C# decimal

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

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

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

C# OnMeasureItem

1. **整体功能概述**   - `OnMeasureItem` 是一个在Wi...

c# Invalidate、Refresh、Refreshitem、Refreshitems的功能

Invalidate方法功能概述Invalidate方法主要用于使控件的特定区域(整个控件或部分区域...