QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

查看: 1841|回复: 0

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

[复制链接]

等级头衔

积分成就    金币 : 2806
   泡泡 : 1516
   精华 : 6
   在线时间 : 1244 小时
   最后登录 : 2024-5-5

丰功伟绩

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

联系方式
发表于 2022-10-19 09:53:45 | 显示全部楼层 |阅读模式
概述. ^4 K$ U% W3 C
       计算一段程序的耗时是我们在编程中很常见的用法,那这节内容就通过实例的方式来演示几种常用的统计耗时的方法.
2 {! D+ V. x( B0 f方法一:stopwatch
5 Y" l* m: [6 \/ 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.
: {/ w7 R/ y. ?( D运行结果:# w8 b7 S9 ]2 m5 ?: `! A5 J) E
程序耗时:1012ms.. a  ]' w4 p4 c& @0 Q- i
方法二:DateTime.Now
* l! F) E; m) I9 Y2 A+ g. Q
  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.         }
运行结果:% i7 J7 J5 o( t! q; z
程序耗时:1012.2267ms.
6 t- ]* s* n3 Q1 X- D方法三:ValueStopwatch
: E9 f9 z( x% o用法举例如下:
& F# ~& t) S& ~! q) _
  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. }
运行结果:* B0 u) U' l0 ~; g
程序耗时:1008.0426ms.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-17 12:04

Powered by paopaomj X3.4 © 2016-2024 sitemap

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