C# 测试程序运行时间

1.时间测试

DateTime starttime = DateTime.Now;

TimeSpan timespan;

程序主体

timespan = DateTime.Now.Subtract(starttime);//获取就是开始时间很结束时间差

2.用于.NET环境的时间测试(只测试代码在自身进程中的时间)

.NET环境为执行无用单元收集调用提供了专门的对象--GC。为了是系统执行无用单元收集。

GC.Collect();

程序等待堆上对象的所有finalizer方法都运行后再继续。

GC.WaitForPendingFinalizers();

TimeSpan starttime;

starttime = Process.GetCurrentProcess().Threads[0].UserProcessorTime;

程序主体

TimeSpan endtime = Process.GetCurrentProcess().Threads[0].UserProcessorTime.Subtract(starttime);//获取就是开始时间很结束时间差

posted @ 2012-09-16 11:55  Alec-Yin  阅读(1259)  评论(1编辑  收藏  举报
更多前端博客,访问:http://lhyin.com