Skip to content

Instantly share code, notes, and snippets.

@peterhpchen
Last active April 10, 2020 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhpchen/f82bb3f00f6738f432958295c9d3a0c1 to your computer and use it in GitHub Desktop.
Save peterhpchen/f82bb3f00f6738f432958295c9d3a0c1 to your computer and use it in GitHub Desktop.
Stopwatch Demo
using System;
using System.Diagnostics;
using System.Threading;
public class Program
{
public static void Main()
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// 上面兩行可以改為下面一行
// Stopwatch stopWatch = Stopwatch.StartNew();
Thread.Sleep(1000);
stopWatch.Stop();
Console.WriteLine(stopWatch.ElapsedMilliseconds);
stopWatch.Start();
Thread.Sleep(1000);
stopWatch.Stop();
Console.WriteLine(stopWatch.ElapsedMilliseconds); // 因沒有 Reset ,此時間會繼續累加
stopWatch.Reset();
stopWatch.Start();
// 上面兩行可以改為下面一行
// stopWatch.Restart();
Thread.Sleep(1000);
stopWatch.Stop();
Console.WriteLine(stopWatch.ElapsedMilliseconds);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment