个人资料

跳过导航链接首页 > 博客列表 > 博客正文

c#优化启动/运行速度,检测运行时间查看耗时操作

:


C#测试代码运行时间

使用Stopwatch

Stopwatch sw = new Stopwatch();
sw.Start();
//...
sw.Stop();
//需要打开VS输出窗口查看
System.Diagnostics.Debug.WriteLine("时间:" + sw.ElapsedMilliseconds);
sw.Reset(); 

比较好看的写法

  1. System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
    sw.Start();
    {
        //这里是需要检测的程序
    }
    sw.Stop();
    Console.WriteLine(sw.Elapsed);






使用Runtime

Runtime.Restart();
//...
Runtime.Stop();
TimeSpan timespan = Runtime.Elapsed;
System.Diagnostics.Debug.WriteLine("程序使用时间:{0}\n", timespan.TotalMilliseconds);


优化


----

ps 准备优化‘某个’app的启动速度

admin
最初发表2019/9/9 11:13:53 最近更新2019/9/9 13:47:55 2324
为此篇作品打分
10