目录

简单介绍

  Stopwatch 可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间。一般用来测量代码执行所用的时间或者计算性能数据,在优化代码性能上可以使用Stopwatch来测量时间。

基本用法

  使用的时候需要引用 System.Diagnostics 命名空间。先调用 Start 方法,然后调用 Stop 方法,最后使用 Elapsed 属性或者使用 ElapsedMilliseconds 属性得到运行时间(这两个属性的区别是前者得到的是TimeSpan实例,后者得到的是毫秒)。使用 IsRunning 可以确定 Stopwatch 的当前状态是正在运行还是已经停止。每次调用 Start 时开始累计运行时间计数;每次调用 Stop 时结束当前时间间隔测量,并冻结累计运行时间值。 使用 Reset 方法可以清除现有 Stopwatch 实例中的累计运行时间。

  下面的示例演示如何使用 Stopwatch 类来确定应用程序的执行时间:

  

            Stopwatch stopwatch = new Stopwatch();
//第一次计时
stopwatch.Start();
Console.WriteLine("Stopwatch is running:{0}",stopwatch.IsRunning);//判断当前Stopwatch的状态
System.Threading.Thread.Sleep();//耗时操作
stopwatch.Stop();
Console.WriteLine("Using Elapsed output runTime:{0}", stopwatch.Elapsed.ToString());//这里使用时间差来输出
Console.WriteLine("Using ElapsedMilliseconds output runTime:{0}", stopwatch.ElapsedMilliseconds);//这里面使用毫秒来输出
Console.WriteLine("===================================================");
//第二次计时
stopwatch.Start();
System.Threading.Thread.Sleep();//耗时操作
stopwatch.Stop();
Console.WriteLine("The second RunTime:{0}", stopwatch.ElapsedMilliseconds);//这里面使用毫秒来输出
Console.WriteLine("===================================================");
//第三次计时(这里使用了Restart)
stopwatch.Restart();//这里使用Restart来启动计时(会把前面的时间清空)
System.Threading.Thread.Sleep();//耗时操作
stopwatch.Stop();
Console.WriteLine("Using Restart, so runTime:{0}", stopwatch.ElapsedMilliseconds);//这里面使用毫秒来输出
Console.ReadKey();

  运行结果:

   

  备注:程序中我 Sleep 了1000ms 但是输入结果是第一次是1002 第二次是2003 最后是1000,有一定的误差跟电脑性能和当前CPU的状态有关系(这里我是猜测的)。

在实际的项目中,有时候我们无法使用Console.WriteLine() 或者 MessageBox.Show()来得到运行时间,这时候我们就需要把时间写进一个文本文件中。

  下面的示例演示如何使用 Stopwatch 类来确定应用程序的执行时间并把时间输出到文本文件中(这里我为了简单使用的是 StreamWriter):

  

            const string path = @"D:\StopwatchDemo.txt";

            using (StreamWriter streamWriter = new StreamWriter(path, true, Encoding.UTF8))
{
streamWriter.WriteLine("Start");
streamWriter.Flush();
Stopwatch stopwatch = Stopwatch.StartNew();
System.Threading.Thread.Sleep();//耗时操作
stopwatch.Stop();
streamWriter.WriteLine("RunTime:{0}", stopwatch.ElapsedMilliseconds);//以毫秒的形式写进文件
streamWriter.Flush();
}

  运行结果:

  

结尾

    C#中用于计时的操作不只是 Stopwatch 还有其他的如 Timer、TimeSpan 等。我这里只是用Stopwatch来举个例子。  

  

C#知识点-StopWatch-计时的更多相关文章

  1. C# Stopwatch计时统计

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  2. C#中精确计时的一点收获 Stopwatch

    http://www.cnblogs.com/jintianhu/archive/2010/09/01/1815031.html 参考: https://www.cnblogs.com/kissdod ...

  3. C#中精确计时的一点收获 【转】

    C#中精确计时的一点收获 [转] 以下所有代码运行环境:Windows 2003, Intel(R) Core(TM) 2 Duo CPU E8400 @  3.00GHz 2.99GHz,2.96G ...

  4. System.Diagnostics.Stopwatch

    System.Diagnostics.Stopwatch 注意:此类在 .NET Framework 2.0 版中是新增的.MSDN Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量 ...

  5. C#中精确计时的一点收获

    以下所有代码运行环境:Windows 2003, Intel(R) Core(TM) 2 Duo CPU E8400 @  3.00GHz 2.99GHz,2.96GB内存 根据综合网上的一些文章,精 ...

  6. Stopwatch 和TimeSpan介绍【转】

    1.使用 Stopwatch 类 (System.Diagnostics.Stopwatch) Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.在典型的 S ...

  7. C#中各种计时器 Stopwatch、TimeSpan

    1.使用 Stopwatch 类 (System.Diagnostics.Stopwatch)Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.在典型的 St ...

  8. C# Stopwatch 类

    命名空间:System.Diagnostics Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.在典型的 Stopwatch 方案中,先调用 Start 方 ...

  9. C# Stopwatch详解

    namespace System.Diagnostics { // // 摘要: // 提供一组方法和属性,可用于准确地测量运行时间. public class Stopwatch { // // 摘 ...

  10. .NET中System.Diagnostics.Stopwatch、System.Timers.Timer、System.Threading.Timer 的区别

    1.System.Diagnostics.Stopwatch Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间. 在典型的 Stopwatch 方案中,先调用 ...

随机推荐

  1. bzoj 1369: Gem 树形dp

    题目大意 给出一棵树,要求你为树上的结点标上权值,权值可以是任意的正整数 唯一的限制条件是相临的两个结点不能标上相同的权值,要求一种方案,使得整棵树的总价值最小.N<=10000 题解 我们可以 ...

  2. 数据结构与算法(3)----->队列和栈

    1. 栈和队列的基本性质 栈是先进后出;(像是子弹夹,后进先打出) 队列是先进先出;(像是平时排队买冰淇淋,按顺序轮流) 栈和队列在实现的结构上可以有数组和链表两种形式; (1)数组结构实现容易; ( ...

  3. C# 深化基本概念

    关于IDisposable的Dispose方法 .Net中GC会自动回收托管资源, 对于非托管资源应该使用Dispose方法. 在使用Dispose方法时,应注意避免在Dispose内部中继续释放托管 ...

  4. WPF error: does not contain a static 'Main' method suitable for an entry point

    WPF error: does not contain a static &apos;Main&apos; method suitable for an entry point doe ...

  5. WPF Get Multibinding Expression, Update Source,

    wpf 拿到某个control的multibinding以及其中每个Binding 1. 拿到multibinding      MultiBindingExpression mbe = Bindin ...

  6. POJ3660(foyld闭包问题)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8794   Accepted: 4948 Descr ...

  7. caffe Dtype

    http://blog.luoyetx.com/2015/10/reading-caffe-2/

  8. Hibernate"discriminator-value"用法

    转自:https://blog.csdn.net/iteye_3357/article/details/81862615 1 可能经常遇到这样的情况: 2 3 在数据库表中会有这样的一个字段用来区别记 ...

  9. nohup开机自启脚本

    #!/bin/bash cd /root/xcloud/ str=$"/n" sstr=$(echo -e $str) nohup ./deploy >>/dev/nu ...

  10. SAP ECC6 IDES安装及虚拟机下载

    SAP ECC6.0 SR3 IDES Oracle.torrent(48.12G)下载 SAP ECC6 安装系列 SAP ECC6.0 IDES在Win7 X64上的安装 SAP ECC6.0 R ...