Skip to content

Instantly share code, notes, and snippets.

@HongfeiXu
Created August 24, 2018 06:27
Show Gist options
  • Save HongfeiXu/a7d365e3a68ba492df568279b065b7b1 to your computer and use it in GitHub Desktop.
Save HongfeiXu/a7d365e3a68ba492df568279b065b7b1 to your computer and use it in GitHub Desktop.
C# 中,使用 System.Diagnostics.Stopwatch 计时
// https://msdn.microsoft.com/zh-cn/library/system.diagnostics.stopwatch(v=vs.110).aspx
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
static void Main(string[] args)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread.Sleep(10000); // sleep for 10 seconds
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);
// Or format this way
Console.WriteLine("RunTime " + ts.TotalMilliseconds + "ms");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment