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

C# string与StringBuilder速度测试

C#4年前 (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

相关文章

DotfuscatorPro使用教程

DotfuscatorPro使用教程

1首次使用,添加反编译工具路径ILASM_v4.0.30319C:\Windows\Microsof...

C# MDI例子

父窗口属性IsMdiContainer设置为Trueprivate void b...

C# Windows环境下以管理员启动

右键点击项目名称    -   ...

C# 可空参数

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

C# 缩减代码量的一些方式

static void Main() { Thread thre...

C# OnMeasureItem

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