private void ShowRunTime()
{
TimeSpan ts1 = Process.GetCurrentProcess().TotalProcessorTime;
Stopwatch stw = new Stopwatch();
stw.Start();
int Circles = 1000;
for (int i = 0; i < Circles; ++i)
{
Console.WriteLine(i.ToString());
}
double Msecs = Process.GetCurrentProcess().TotalProcessorTime.Subtract(ts1).TotalMilliseconds;
stw.Stop();
Console.WriteLine(string.Format("循环次数:{0} CPU时间(毫秒)={1} 实际时间(毫秒)={2}", Circles, Msecs, stw.Elapsed.TotalMilliseconds, stw.ElapsedTicks));
Console.WriteLine(string.Format("1 tick = {0}毫秒", stw.Elapsed.TotalMilliseconds / stw.Elapsed.Ticks));
}
核心代码:
Stopwatch stw = new Stopwatch();
stw.Start();
string aa= wactch.ElapsedMilliseconds.ToString();
string aa= stw.Elapsed.TotalMilliseconds.ToString();
stw.Stop();