c# - How to calculate the execution time of a method

c# example - calculate execution time of a method
calculate-execution-time-of-a-method.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>
<script runat="server">
    static public int add(int x, int y)
    {
        int result = x + y;
        return result;
    }
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        
        Label1.Text = "";
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

        for (int i = 0; i < 10;i++ )
        {
            sw.Start();
            int output = add(50, i);
            Label1.Text += output;
            sw.Stop();
            string outputs = String.Format("Time elapsed: {0}ms", sw.Elapsed.TotalMilliseconds);
            Label1.Text += " | " + outputs + "<br />";
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>c# example - calculate execution time of a method</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkBlue; font-style:italic;">
            c# example - calculate execution time
            <br /> of a function/method
        </h2>  
        <hr width="550" align="left" color="LightBlue" />  

        <asp:Label 
            ID="Label1" 
            runat="server"
            Font-Size="XX-Large"
            >
        </asp:Label>
        <br />
        <asp:Button 
            ID="Button1" 
            runat="server" 
            Text="Calculate Execution Time Of A Method" 
            OnClick="Button1_Click"
            Height="40"
            Font-Bold="true"
            />
    </div>
    </form>
</body>
</html>