C#使用记秒表检查程序运行时间

C#使用记秒表检查程序运行时间,第1张

概述C#使用记秒表检查程序运行时间

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

如果你担忧某些代码非常耗费时间,可以用StopWatch来检查这段代码消耗的时间,如下面的代码所示
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();timer.Start();Decimal total = 0;int limit = 1000000;for (int i = 0; i < limit; ++i){  total = total + (Decimal)Math.Sqrt(i);}timer.Stop();Console.Writeline(“Sum of sqrts: {0}”,total);Console.Writeline(“Elapsed milliseconds: {0}”,timer.ElapsedMilliseconds);Console.Writeline(“Elapsed time: {0}”,timer.Elapsed);

class autoStopwatch : System.Diagnostics.Stopwatch,Idisposable{   public autoStopwatch()   {      Start();   }   public voID dispose()   {     Stop();     Console.Writeline(“Elapsed: {0}”,this.Elapsed);   }}

借助于using语法,像下面的代码所示,可以检查一段代码的运行时间,并打印在控制台上。
using (new autoStopwatch()){  Decimal total2 = 0;  int limit2 = 1000000;  for (int i = 0; i < limit2; ++i)  {     total2 = total2 + (Decimal)Math.Sqrt(i);  }}

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的C#使用记秒表检查程序运行时间全部内容,希望文章能够帮你解决C#使用记秒表检查程序运行时间所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1237653.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存