When we jump into performance of our application. Its essential to track which of the code segment is taking more time. To track the time taken by your code or your query you can use System.Diagnostics.Stopwatch instance before execution started of the specific code. And after completion of the code execution calculate the time elapsed. It will shows you the time taken by your application. Sample Code:: System.Diagnostics.Stopwatch sWatch = newSystem.Diagnostics.Stopwatch(); sWatch.Start(); // Your Code Segment. sWatch.Stop(); Now sWatch.ElapsedMilliseconds will give you the execution time of the piece of code in millisecond. |
Sample ASP.NET Application ASPX: |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SpeedTest.aspx.cs" Inherits="SpeedTest" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Test Speed of the code</title> </head> <body> <form id="form1" runat="server"> <div> <%-- To Display the elapsed time--%> <asp:Label ID="lblShowTime" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
Code Behind:
public partial class SpeedTest : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { System.Diagnostics.Stopwatch sWatch = new System.Diagnostics.Stopwatch(); sWatch.Start(); System.Threading.Thread.Sleep(400); sWatch.Stop(); lblShowTime.Text = sWatch.ElapsedMilliseconds.ToString(); } }
Yes. its a great for point. Thank you Aditya.