一:
DateTime BeginTime = System.DateTime.Now;
//代码
DateTime EndTime = System.DateTime.Now;
TimeSpan ts = EndTime.Subtract(BeginTime);
MessageBox.Show(ts.TotalMilliseconds.ToString());
二:
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Reset();
watch.Start();
//代码
watch.Stop();
MessageBox.Show(watch.ElapsedMilliseconds.ToString());