在C#中有一个秒表类:stopwatch,用这个类可以方便的测试一下代码运行时间。要使用stopwatch要先加一个命名空间,System.Diagnostics。具体用法如下:

            Stopwatch timer = new Stopwatch();//new一个stopwatch
            long total = 0;
            timer.Start();//开始计算时间
            for (long i = 1; i <= 100000000; i++)
            {
                total += i;
            }
            timer.Stop();//结束点,另外stopwatch还有Reset方法,可以重置。

            Console.WriteLine(timer.Elapsed);//显示时间
            Console.WriteLine(timer.ElapsedMilliseconds);// 显示到毫秒

相关文章:

  • 2021-09-30
  • 2022-02-06
  • 2022-12-23
猜你喜欢
  • 2021-07-16
  • 2021-10-09
  • 2022-12-23
  • 2022-12-23
  • 2022-02-03
  • 2021-09-26
  • 2022-01-28
相关资源
相似解决方案