When programming, you often want to test the execution time of your own page, especially during the Asp.net test,
To solve this problem, I will summarize the method for loading the execution time on the page for your learning!
1. obtain from the server
1. Calculate the time in the global. asax File
Datetime DT;
Protected void application_beginrequest (Object sender, eventargs E)
{
Dt = datetime. now;
}
Protected void application_endrequest (Object sender, eventargs E)
{
Datetime dt2 = datetime. now;
Timespan Ts = dt2-dT;
Response. Write ("page loading time" + ts. TotalMilliseconds. ToString () + "millisecond ");
}
2. Add the following method to Aspx. CS:
Protected DateTime dt;
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected override void OnPreInit (EventArgs e)
{
Dt = DateTime. Now;
Base. OnPreInit (e );
}
Protected override void onloadcomplete (eventargs E)
{
Base. onloadcomplete (E );
Timespan Ts = datetime. Now-dT;
Response. Write ("when loading the page:" + ts. totalmilliseconds );
}
Ii. Loading time from the client
The procedure is as follows:
1. Add the initial time in the head part of the page
<Script language = JavaScript>
VaR T1 = new date (). gettime ();
</SCRIPT>
2. Add the following content to the body:
<Script language = "JavaScript">
Window. onload = function ()
{
Document. getelementbyid ("timeshow"). innerhtml = "loading this page time" + (new date (). gettime ()-t1) + "millisecond ";
}
</SCRIPT>
3. Insert a tag with the ID of timeshow to be displayed.
<Div id = "timeshow"> </div>