標籤:
一,先開啟開始或繼續測量某個時間間隔的已耗用時間,然後停止,最後重設時間,輸出.
using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class TestPerformance : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { /*Var 效能測試 */ Stopwatch stw = new Stopwatch(); List<long> intlist = new List<long>(); stw.Start(); for (long i = 0; i < 10000000; i++) { var index = i;//使用var轉入和轉出 intlist.Add(index); } stw.Stop(); Label1.Text= stw.Elapsed.Ticks.ToString(); stw.Reset(); /*Int 效能測試 */ List<long> intlist2 = new List<long>(); stw.Start();//開始時間 for (long i = 0; i < 1000000; i++) { long index = i;//使用int轉入和轉出 intlist2.Add(index); } stw.Stop(); Label2.Text = stw.Elapsed.Ticks.ToString(); stw.Reset(); /*Object效能測試 */ stw.Start();//開始時間 List<long> intlist3 = new List<long>(); for (long i = 0; i < 1000000; i++) { object index = i;//使用object轉入和轉出 intlist3.Add((long)index); } stw.Stop(); Label3.Text = stw.Elapsed.Ticks.ToString(); }}
二,最上層顯示
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPerformance.aspx.cs" Inherits="TestPerformance" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title></head><body> <form id="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></html>
三,顯示結果
C#Stopwatch的使用,效能測試