First, start or continue to measure the elapsed time of an interval, then stop, last reset time, output.
usingSystem;usingSystem.Collections.Generic;usingSystem.Diagnostics;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls; Public Partial classtestperformance:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) { /*Var Performance Test*/Stopwatch STW=NewStopwatch (); List<Long> intlist =Newlist<Long>(); StW. Start (); for(Longi =0; I <10000000; i++) { varindex = i;//use Var to transfer and transfer outintlist. ADD (index); } STW. Stop (); Label1.Text=STW. Elapsed.Ticks.ToString (); StW. Reset (); /*Int Performance Test*/List<Long> intlist2 =Newlist<Long>(); StW. Start ();//Start Time for(Longi =0; I <1000000; i++) { Longindex = i;//use int to transfer and transfer outIntlist2. ADD (index); } STW. Stop (); Label2.Text=STW. Elapsed.Ticks.ToString (); StW. Reset (); /*Object Performance Test*/STW. Start ();//Start TimeList<Long> intlist3 =Newlist<Long>(); for(Longi =0; I <1000000; i++) { Objectindex = i;//use object to transfer and transfer outIntlist3. ADD (Long) (index); } STW. Stop (); Label3.text=STW. Elapsed.Ticks.ToString (); }}
Second, front-end display
<%@ Page language="C #"autoeventwireup="true"codefile="TestPerformance.aspx.cs"inherits="testperformance"%><! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Server"><meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/> <title></title>"Form1"runat="Server"> <div>Var:<asp:label id="Label1"runat="Server"text="Var"></asp:Label><br/>Int:<asp:label id="Label2"runat="Server"text="Int"></asp:Label><br/>Object:<asp:label id="Label3"runat="Server"text="Object"></asp:Label> </div> </form></body>Three, show the result
Use of C#stopwatch, performance testing