ASP.NET中计算页面执行所耗时间收藏[转载]
在asp.net中计算页面执行所耗时间
主要是重写 Render方法
DateTime start;
void Page_Init (Object sender, EventArgs e)
{
start = DateTime.Now;
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
DateTime stop = DateTime.Now;
Response.Write("done in " + (stop-start));
}
方法二:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
Application["StartTime"] = System.DateTime.Now;
}
protected void Application_EndRequest(Object sender, EventArgs e)
{
System.DateTime startTime = (System.DateTime)Application["StartTime"];
System.DateTime endTime = System.DateTime.Now;
System.TimeSpan ts = endTime - startTime;
Response.Write("页面执行时间:"+ ts.Milliseconds +" 毫秒");
}
代码直接copy到Global.asax就可以运行了。