QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

泡泡马甲APP 更多内容请下载泡泡马甲手机客户端APP 立即下载 ×
查看: 1826|回复: 0

[C#] C# 计算耗时的三种方法

[复制链接]

等级头衔

积分成就    金币 : 2802
   泡泡 : 1516
   精华 : 6
   在线时间 : 1242 小时
   最后登录 : 2024-4-18

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老

联系方式
发表于 2022-10-19 09:53:45 | 显示全部楼层 |阅读模式
概述# {7 }  k$ y& s, {' K/ T* \
       计算一段程序的耗时是我们在编程中很常见的用法,那这节内容就通过实例的方式来演示几种常用的统计耗时的方法.
5 p) N! s/ p% @; u方法一:stopwatch1 D2 E9 y8 C& u: A; A
  1.   static void Main(string[] args)
  2.         {
  3.             Stopwatch sw = new Stopwatch();
  4.             sw.Start();
  5.             Thread.Sleep(999);
  6.             sw.Stop();
  7.             Console.WriteLine($"程序耗时:{sw.ElapsedMilliseconds}ms.");
  8.             Console.ReadKey();
  9.         }
      最常用的计算耗时的就是使用Stopwatch,使用的时候需要引用命名空间:System.Diagnostics.
9 M4 D0 O2 S2 I/ ^8 v) j运行结果:
9 |; I2 w) \7 x5 R: M程序耗时:1012ms.3 d+ L. e0 Y' w; M, n' Z6 f- Z
方法二:DateTime.Now) U: _! b& |/ H% {  v# P
  1. static void Main(string[] args)
  2.         {
  3.             //Stopwatch sw = new Stopwatch();
  4.             //sw.Start();
  5.             //Thread.Sleep(999);
  6.             //sw.Stop();
  7.             //Console.WriteLine($"程序耗时:{sw.ElapsedMilliseconds}ms.");
  8.             var start = DateTime.Now;
  9.             Thread.Sleep(999);
  10.             var stop = DateTime.Now;
  11.             Console.WriteLine($"程序耗时:{(stop - start).TotalMilliseconds}ms.");
  12.             Console.ReadKey();
  13.         }
运行结果:0 ^. C' G- I/ w; M
程序耗时:1012.2267ms.& s' E* a. w! G9 C7 k& @! R
方法三:ValueStopwatch2 C2 R9 C. [+ x# K( G% I2 M. j; p3 H
用法举例如下:
$ M2 k+ E6 x) u6 y2 X4 H- \+ \
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. namespace ConsoleApp27
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             //Stopwatch sw = new Stopwatch();
  11.             //sw.Start();
  12.             //Thread.Sleep(999);
  13.             //sw.Stop();
  14.             //Console.WriteLine($"程序耗时:{sw.ElapsedMilliseconds}ms.");
  15.             //var start = DateTime.Now;
  16.             //Thread.Sleep(999);
  17.             //var stop = DateTime.Now;
  18.             //Console.WriteLine($"程序耗时:{(stop - start).TotalMilliseconds}ms.");
  19.             var watch = ValueStopwatch.StartNew();
  20.             Thread.Sleep(999);
  21.             Console.WriteLine($"程序耗时:{watch.GetElapsedTime().TotalMilliseconds}ms.");
  22.             Console.ReadKey();
  23.         }
  24.     }
  25.     internal struct ValueStopwatch
  26.     {
  27.         private static readonly double TimestampToTicks = TimeSpan.TicksPerSecond / (double)Stopwatch.Frequency;
  28.         private readonly long _startTimestamp;
  29.         public bool IsActive => _startTimestamp != 0;
  30.         private ValueStopwatch(long startTimestamp)
  31.         {
  32.             _startTimestamp = startTimestamp;
  33.         }
  34.         public static ValueStopwatch StartNew() => new ValueStopwatch(Stopwatch.GetTimestamp());
  35.         public TimeSpan GetElapsedTime()
  36.         {
  37.             // Start timestamp can't be zero in an initialized ValueStopwatch. It would have to be literally the first thing executed when the machine boots to be 0.
  38.             // So it being 0 is a clear indication of default(ValueStopwatch)
  39.             if (!IsActive)
  40.             {
  41.                 throw new InvalidOperationException("An uninitialized, or 'default', ValueStopwatch cannot be used to get elapsed time.");
  42.             }
  43.             var end = Stopwatch.GetTimestamp();
  44.             var timestampDelta = end - _startTimestamp;
  45.             var ticks = (long)(TimestampToTicks * timestampDelta);
  46.             return new TimeSpan(ticks);
  47.         }
  48.     }
  49. }
运行结果:
1 L: z" D" c. J5 O4 W0 X程序耗时:1008.0426ms.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号 )

GMT+8, 2024-4-27 20:40

Powered by paopaomj X3.4 © 2016-2024 sitemap

快速回复 返回顶部 返回列表