using System.Diagnostics;
using UnityEngine;
using System.Text;
public class test : MonoBehaviour {
/*
//代码执行计时器,需要引入System.Diagnostics命名空间
Stopwatch sw = new Stopwatch();
sw.Start();//开始记时
sw.Stop();//结束计时
sw.Elapsed//记录的时间
//StringBuilder 修改字符串 效率极高。需要引入System.Text;命名空间
StringBuilder sb = new StringBuilder();
sb.Append(string)//向stringBuilder中添加字符
sb.Clear();//清空stringbuider中的内容
*/
void Start ()
{
//代码执行计时器,需要引入System.Diagnostics命名空间
Stopwatch sw = new Stopwatch();
sw.Start();//开始记时
string names = "";
for (int i = 0; i <= 90000; i++)
{
names += "LX";
}
//StringBuilder sb = new StringBuilder();
//for (int i = 0; i < 90000; i++)
//{
// sb.Append("LX");
//}
sw.Stop();//结束计时
print(sw.Elapsed);//1:string 39.125
//2:stringbuilder 0.001
}
}
C#代码优化StringBundle和Stopwatch代码运行计时
最新推荐文章于 2021-07-02 10:44:34 发布