C# Stopwatch

原文链接:https://blog.csdn.net/TheWindofFate/article/details/122882621

一、什么是Stopwatch

Stopwatch:提供一组方法和属性,可以准确的测量运行时间。使用的时候需要引用命名空间:System.Diagnostics。

二、Stopwatch的简单使用

//创建Stopwatch实例
Stopwatch sw = new Stopwatch();
//开始计时
sw.Start();
for (int i = 0; i < 100; i++)
{
Console.WriteLine(i);
}
//停止计时
sw.Stop();
Console.WriteLine("用时:" + sw.ElapsedMilliseconds + "");
//重置 停止时间间隔测量,并将运行时间重置为0
sw.Reset();
Console.WriteLine("用时:" + sw.ElapsedMilliseconds + "");
//重启 停止时间间隔测量,并将运行时间重置为0,然后重新开始测量运行时间
sw.Restart();
for (int i = 0; i < 100; i++)
{
Console.WriteLine(i);
}
sw.Stop();
//获取当前实例测量得出的总运行时间(以毫秒为单位)
Console.WriteLine("用时:" + sw.ElapsedMilliseconds + "");
//获取当前实例测量得出的总运行时间
Console.WriteLine("用时:"+sw.Elapsed);
//获取当前实例测量得出的总运行时间(用计时器刻度表示)。
Console.WriteLine(sw.ElapsedTicks);
Console.Read();

  

posted @ 2024-02-05 17:10  yinghualeihenmei  阅读(194)  评论(0编辑  收藏  举报