Stopwatch sw; sw = new Stopwatch(); sw.Start(); //待计算运行时间的代码 sw.Stop(); TimeSpan ts2 = sw.Elapsed; MessageBox.Show(ts2.TotalMilliseconds.ToString());