zoukankan      html  css  js  c++  java
  • .net 中进行消耗时间计时

    在项目中,经常要对某些方法的执行性能(消耗的时间)进行日志记录,有两种方案来实现。

    StopWatch

    使用Stopwatch类来量度时间非常简单。跟现实生活中的秒表一样,这个类的对象也能够对计数器进行开始、停止、归零(重置)操作,不过它可比一般的秒表精确多了,它能够精确到微秒(也就是百万分之一秒)。

    (以下的示例来自于 http://www.cnblogs.com/greatandforever/archive/2008/07/23/1249185.html)

    要演示Stopwatch的使用还是来段代码吧。下面是一个控制台应用程序,它将1到100万之间的所有整数累加:

    using System;
    
    namespace StopWatchClass
    {
        class Program
        {
            static void Main(string[] args)
            {
                long total = 0;
                for (int i = 1; i <= 10000000; i++)
                {
                    total += i;
                }
            }
        }
    }

    添加 Stopwatch 对象

    Stopwatch类位于System.Diagnostics命名空间。下面是添加对象后的代码:

    using System;
    using System.Diagnostics;
    
    namespace StopWatchClass
    {
        class Program
        {
            static void Main(string[] args)
            {
                Stopwatch timer = new Stopwatch();
                long total = 0;
                for (int i = 1; i <= 10000000; i++)
                {
                    total += i;
                }
            }
        }
    }

    控制 Stopwatch 对象

    Stopwatch提供了几个方法用以控制Stopwatch对象。Start方法开始一个计时操作,Stop方法停止计时。此时如果第二次使用 Start方法,将继续计时,最终的计时结果为两次计时的累加。为避免这种情况,在第二次计时前用Reset方法将对象归零。这三个方法都不需要参数。代码是:

    using System;
    using System.Diagnostics;
    
    namespace StopWatchClass
    {
        class Program
        {
            static void Main(string[] args)
            {
                Stopwatch timer = new Stopwatch();
                long total = 0;
                timer.Start();
                for (int i = 1; i <= 10000000; i++)
                {
                    total += i;
                }
                timer.Stop();
            }
        }
    }

    读取 Stopwatch 结果

    在结束计时后下一步就是读取计时结果了。Stopwatch类提供了以下属性:

    • Elapsed:返回一个TimeSpan对象,表示计时时间间隔;
    • ElapsedMilliseconds:返回计时经过的微秒数,精确度稍差,适合于稍长一点的计时;
    • ElapsedTicks: 返回计时经过的计时器刻度(timer tick)数。计时器刻度是Stopwatch对象可能的最小量度单位。计时器刻度时间的长度由特定的计算机和操作系统确定。Stopwatch对象的 Frequency静态字段的值表示一秒所包含的计时器刻度数。注意它与TimeSpan的Ticks属性所用的时间单位的区别。

    应当根据计时任务的情况选择其中的一个属性。在我们的示例程序中,Elapsed属性提供了需要的精确度,用它来输出经过的微秒数。这也是TimeSpan的最高精确度了。
    下面是最终的程序代码:

    using System;
    using System.Diagnostics;
    
    namespace StopWatchClass
    {
        class Program
        {
            static void Main(string[] args)
            {
                Stopwatch timer = new Stopwatch();
                long total = 0;
    
                timer.Start();
                for (int i = 1; i <= 10000000; i++)
                {
                    total += i;
                }
    
                timer.Stop();
    
                decimal micro = timer.Elapsed.Ticks / 10m;
                Console.WriteLine("Execution time was {0:F1} microseconds.", micro);
            }
        }
    }

    另外,使用IsRunning属性可以查看一个Stopwatch实例是否正在计时,使用StartNew方法可以开始一个新的计时器。

    DateTime.Now.Ticks

    使用 DateTime.Now.Ticks 可以得到当前时刻的毫微秒数。在执行方法前使用变量1存储当前毫微秒,执行方法后再用变量2存储当前毫微秒,则相应的时间差,转换为秒的话为:

    时间间隔(秒) = (变量2 - 变量1) * 10000000d

  • 相关阅读:
    Iphone开发-NSdata 与 NSString,Byte数组,UIImage 的相互转换
    Iphone访问WCF服务 之 ASIHTTPRequest
    TP6 实现上传文件
    页面自适应纯CSS,使用rem单位
    jQuery解决高度统一问题
    dedecms Ajax异步获取文章列表
    手机端页面自适应解决方案—rem布局
    mysql中bigint、int、mediumint、smallint 和 tinyint的取值范围
    修改PHP 上传文件大小限制
    WampServer下修改和重置MySQL密码
  • 原文地址:https://www.cnblogs.com/xiefang2008/p/9552137.html
Copyright © 2011-2022 走看看