方法一:stopwatch
using System.Diagnostics;
public class Test
{
static void Main(string[] args)
{
Stopwatch swt = new Stopwatch();
swt.Start();
Thread.Sleep(999);
swt.Stop();
Console.WriteLine("程序耗时为:"+swt.ElapsedMilliseconds +"ms");
Console.ReadKey();
}
}
方法二:DateTime.Now
using System;
public class Test
{
static void Main(string[] args)
{
var start = DateTime.Now;
Thread.Sleep(999);
var stop = DateTime.Now;
Console.WriteLine("程序耗时为:"+(stop - start).TotalMilliseconds+"ms");
Console.ReadKey();
}
}