1.用DateTime
long startTime = DateTime.Now.Ticks;
f();
long endTime = DateTime.Now.Ticks;
float lastTime = (float) (endTime - startTime) / 10000; //转化为毫秒
Console.Write("程序花费的毫秒数:");
Console.WriteLine(lastTime);
2.用Stopwatch类
using System.Diagnostics;//引用相关的命名空间
Stopwatch st = new Stopwatch();//实例化类
st.Start();//开始计时
f();//调用某函数
st.Stop();//终止计时
Console.Write("程序花费的毫秒数:");
Console.WriteLine(st.ElapsedMilliseconds.ToString());