运行时间计算
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); //开始监视代码运行时间 //需要测试的代码 watch.Stop(); //停止监视 TimeSpan timespan = watch.Elapsed; //获取当前实例测量得出的总时间 System.Diagnostics.Debug.WriteLine("打开窗口代码执行时间:{0}(毫秒)", timespan.TotalMilliseconds); //总毫秒数
计算内存分配
/// <summary> /// 使用方法 /// </summary> /// <param name="args"></param> static void Main(string[] args) { MemoryWatcher watcher = new MemoryWatcher(); watcher.startWatch(); object obj = new object().GCWatch("终于释放啦"); watcher.stoptWatch(); Console.ReadKey(); } /// <summary> /// 监视类 /// </summary> public class MemoryWatcher { long size; public void startWatch() { size = 0; size = GC.GetTotalMemory(true); } public void stoptWatch() { var latersize = GC.GetTotalMemory(true); latersize = latersize - size; Console.WriteLine("分配内存:"+ (latersize / 1024).ToString() + "KB"); System.Diagnostics.Debug.WriteLine("分配内存:"+(latersize/1024).ToString()+"KB"); //总毫秒数 } }
时间: 2024-10-22 01:13:08