ASPX Performance - Slow?

Discussion in 'ASP .Net' started by BlueBall, May 13, 2004.

  1. BlueBall

    BlueBall Guest

    I am writing some kind of network testing tool and I have wrote the
    following code in ASP.NET with C#

    int size= 10048576; // around 10 MB data
    string buffer = "";
    for (int j=1; j<=1024; j++) {
    buffer = String.Concat(buffer, "x");
    // I tried buffer += "x"; same performance
    }

    for (int i=1; i<=(size/1024); i++) {
    Response.Write(buffer);
    // I tried using <asp:literal> same thing
    }

    From another computer, I downloaded this file by making that as a link in
    HTML. It was so slow.. I get around 8 Mbps on a 100 Mbps LAN.. In order
    to compare, I tested with a real JPG file I got 25 Mbps! Also, I wrote the
    same thing in PHP using exactly same algorithm. I got around 25 Mbps!

    Anyone knows why ASPX performance is slow? is it configruation problem?
    IIS? or is it Response.write not good? I also tried using <asp: literal>
    same thing.. help..

    Thanks in advance
     
    BlueBall, May 13, 2004
    #1
    1. Advertising

  2. Try your test and replace String.Concat with a StringBuilder. Basically in
    your current code you are doing string allocations on each pass of the loop.
    So what you want is something like:

    int size= 10048576; // around 10 MB data
    System.Text.StringBuilder sb=new System.Text.StringBuilder()
    string buffer = "";
    for (int j=1; j<=1024; j++) {
    sb.Append("x");
    // I tried buffer += "x"; same performance
    }
    buffer=sb.ToString();

    hth
    Chris Torgerson

    "BlueBall" <> wrote in message
    news:eek:2Qoc.62265$...
    > I am writing some kind of network testing tool and I have wrote the
    > following code in ASP.NET with C#
    >
    > int size= 10048576; // around 10 MB data
    > string buffer = "";
    > for (int j=1; j<=1024; j++) {
    > buffer = String.Concat(buffer, "x");
    > // I tried buffer += "x"; same performance
    > }
    >
    > for (int i=1; i<=(size/1024); i++) {
    > Response.Write(buffer);
    > // I tried using <asp:literal> same thing
    > }
    >
    > From another computer, I downloaded this file by making that as a link in
    > HTML. It was so slow.. I get around 8 Mbps on a 100 Mbps LAN.. In order
    > to compare, I tested with a real JPG file I got 25 Mbps! Also, I wrote the
    > same thing in PHP using exactly same algorithm. I got around 25 Mbps!
    >
    > Anyone knows why ASPX performance is slow? is it configruation problem?
    > IIS? or is it Response.write not good? I also tried using <asp: literal>
    > same thing.. help..
    >
    > Thanks in advance
    >
    >
    >
    >
    >
     
    Chris Torgerson, May 13, 2004
    #2
    1. Advertising

  3. String concatenation, whether you user String.Concat or +=, is
    incredibly expensive because it makes a new copy of the string every
    time. Try this instead:

    int size= 10048576; // around 10 MB data
    StringBuilder buffer = new StringBuilder(size);
    for (int j=1; j<=size; j++)
    {
    buffer.Append("x");
    }
    Response.Write(buffer.ToString());

    -Jason


    BlueBall wrote:

    > I am writing some kind of network testing tool and I have wrote the
    > following code in ASP.NET with C#
    >
    > int size= 10048576; // around 10 MB data
    > string buffer = "";
    > for (int j=1; j<=1024; j++) {
    > buffer = String.Concat(buffer, "x");
    > // I tried buffer += "x"; same performance
    > }
    >
    > for (int i=1; i<=(size/1024); i++) {
    > Response.Write(buffer);
    > // I tried using <asp:literal> same thing
    > }
    >
    > From another computer, I downloaded this file by making that as a link in
    > HTML. It was so slow.. I get around 8 Mbps on a 100 Mbps LAN.. In order
    > to compare, I tested with a real JPG file I got 25 Mbps! Also, I wrote the
    > same thing in PHP using exactly same algorithm. I got around 25 Mbps!
    >
    > Anyone knows why ASPX performance is slow? is it configruation problem?
    > IIS? or is it Response.write not good? I also tried using <asp: literal>
    > same thing.. help..
    >
    > Thanks in advance
    >
    >
    >
    >
    >
     
    Jason DeFontes, May 13, 2004
    #3
  4. BlueBall

    BlueBall Guest

    StringBuilder doesn't help because in my code, I don't actually repeat the
    buffer generating part.

    Try to run it on your IIS, you will see what I mean. In order to verify
    this is an ASPX performance problem (or possible my coding algorithm or
    config problem), I have created an old fashion ASP page:

    <%
    dim i, j, size, buffer
    size= 10048576
    for i = 1 to 1024
    buffer = buffer & "x"
    next
    for j = 1 to (size / 1024)
    response.write(buffer)
    next
    %>

    Create a hyper link in a new HTML page and link to this ASP page, and then
    create another link that links to ASPX page you wrote (or the one I posted
    earlier). You can see the BIG difference in performance. ASP page is
    faster than ASPX page at least 3 times... I am frustrated!!!

    B.B.



    "Jason DeFontes" <> wrote in message
    news:%...
    > String concatenation, whether you user String.Concat or +=, is
    > incredibly expensive because it makes a new copy of the string every
    > time. Try this instead:
    >
    > int size= 10048576; // around 10 MB data
    > StringBuilder buffer = new StringBuilder(size);
    > for (int j=1; j<=size; j++)
    > {
    > buffer.Append("x");
    > }
    > Response.Write(buffer.ToString());
    >
    > -Jason
    >
    >
    > BlueBall wrote:
    >
    > > I am writing some kind of network testing tool and I have wrote the
    > > following code in ASP.NET with C#
    > >
    > > int size= 10048576; // around 10 MB data
    > > string buffer = "";
    > > for (int j=1; j<=1024; j++) {
    > > buffer = String.Concat(buffer, "x");
    > > // I tried buffer += "x"; same performance
    > > }
    > >
    > > for (int i=1; i<=(size/1024); i++) {
    > > Response.Write(buffer);
    > > // I tried using <asp:literal> same thing
    > > }
    > >
    > > From another computer, I downloaded this file by making that as a link

    in
    > > HTML. It was so slow.. I get around 8 Mbps on a 100 Mbps LAN.. In

    order
    > > to compare, I tested with a real JPG file I got 25 Mbps! Also, I wrote

    the
    > > same thing in PHP using exactly same algorithm. I got around 25 Mbps!
    > >
    > > Anyone knows why ASPX performance is slow? is it configruation problem?
    > > IIS? or is it Response.write not good? I also tried using <asp:

    literal>
    > > same thing.. help..
    > >
    > > Thanks in advance
    > >
    > >
    > >
    > >
    > >
     
    BlueBall, May 13, 2004
    #4
  5. BlueBall

    Ed Courtenay Guest

    BlueBall wrote:
    > StringBuilder doesn't help because in my code, I don't actually repeat the
    > buffer generating part.
    >
    > Try to run it on your IIS, you will see what I mean. In order to verify
    > this is an ASPX performance problem (or possible my coding algorithm or
    > config problem), I have created an old fashion ASP page:
    >
    > <%
    > dim i, j, size, buffer
    > size= 10048576
    > for i = 1 to 1024
    > buffer = buffer & "x"
    > next
    > for j = 1 to (size / 1024)
    > response.write(buffer)
    > next
    > %>
    >
    > Create a hyper link in a new HTML page and link to this ASP page, and then
    > create another link that links to ASPX page you wrote (or the one I posted
    > earlier). You can see the BIG difference in performance. ASP page is
    > faster than ASPX page at least 3 times... I am frustrated!!!
    >
    > B.B.
    >


    I think I can see where your problem is.

    By default, ASP does not buffer output whereas ASP.NET does. Therefore,
    your ASP code is sending the response directly to the client, whereas
    ASP.Net is building the entire output before sending.

    Try setting Response.BufferOuput to false in your Page.Load event
    handler and then try your test again.






    --

    Ed Courtenay
    [MCP, MCSD]
    http://www.edcourtenay.co.uk
     
    Ed Courtenay, May 13, 2004
    #5
  6. BlueBall

    BlueBall Guest

    I tried adding it but that makes the performance even worse...it's like
    crawling. I am posting my 2 pieces of codes, so that anyone can try it on
    your server. Thanks in advance!

    Create a HTML page and make 2 hyper links pointing to these 2 files, then
    Right-click -> save target as -> You will see the BIG difference between the
    performance between the two.

    -------------------------------------------------------
    test.aspx
    -------------------------------------------------------

    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.IO" %>
    <script language="C#" runat="server">
    void Page_Load() {

    // Response.BufferOutput = false;
    int size= 10485760; // around 10 MB data
    StringBuilder buffer = new StringBuilder(size);
    String str = "";

    for (int i=1; i<=1024; i++)
    buffer.Append("x");

    str = buffer.ToString();

    for (int j=1; j<=size/1024; j++)
    Response.Write(str);


    } // load
    </script>

    -------------------------------------------------------
    test.asp
    -------------------------------------------------------

    <%
    dim i, j, size, buffer
    size = 10485760

    for i = 1 to 1024
    buffer = buffer & "x"
    next

    for j = 1 to (size / 1024)
    response.write(buffer)
    next

    %>

    -------------------------------------------------------





    "Ed Courtenay" <> wrote in
    message news:%...
    > BlueBall wrote:
    > > StringBuilder doesn't help because in my code, I don't actually repeat

    the
    > > buffer generating part.
    > >
    > > Try to run it on your IIS, you will see what I mean. In order to verify
    > > this is an ASPX performance problem (or possible my coding algorithm or
    > > config problem), I have created an old fashion ASP page:
    > >
    > > <%
    > > dim i, j, size, buffer
    > > size= 10048576
    > > for i = 1 to 1024
    > > buffer = buffer & "x"
    > > next
    > > for j = 1 to (size / 1024)
    > > response.write(buffer)
    > > next
    > > %>
    > >
    > > Create a hyper link in a new HTML page and link to this ASP page, and

    then
    > > create another link that links to ASPX page you wrote (or the one I

    posted
    > > earlier). You can see the BIG difference in performance. ASP page is
    > > faster than ASPX page at least 3 times... I am frustrated!!!
    > >
    > > B.B.
    > >

    >
    > I think I can see where your problem is.
    >
    > By default, ASP does not buffer output whereas ASP.NET does. Therefore,
    > your ASP code is sending the response directly to the client, whereas
    > ASP.Net is building the entire output before sending.
    >
    > Try setting Response.BufferOuput to false in your Page.Load event
    > handler and then try your test again.
    >
    >
    >
    >
    >
    >
    > --
    >
    > Ed Courtenay
    > [MCP, MCSD]
    > http://www.edcourtenay.co.uk
     
    BlueBall, May 13, 2004
    #6
  7. Well, I tried it, and your ASP was about 3x *slower* than your ASPX, and
    my ASPX with the StringBuilder and only 1 call to Response.Write,
    instead of your ~10000 calls, was about 1/3 faster than your ASPX. So
    who knows... I imagine there are many, many factors that could affect
    this, and our two samples hardly make a conclusive data set.

    -Jason

    BlueBall wrote:

    > StringBuilder doesn't help because in my code, I don't actually repeat the
    > buffer generating part.
    >
    > Try to run it on your IIS, you will see what I mean. In order to verify
    > this is an ASPX performance problem (or possible my coding algorithm or
    > config problem), I have created an old fashion ASP page:
    >
    > <%
    > dim i, j, size, buffer
    > size= 10048576
    > for i = 1 to 1024
    > buffer = buffer & "x"
    > next
    > for j = 1 to (size / 1024)
    > response.write(buffer)
    > next
    > %>
    >
    > Create a hyper link in a new HTML page and link to this ASP page, and then
    > create another link that links to ASPX page you wrote (or the one I posted
    > earlier). You can see the BIG difference in performance. ASP page is
    > faster than ASPX page at least 3 times... I am frustrated!!!
    >
    > B.B.
    >
    >
    >
    > "Jason DeFontes" <> wrote in message
    > news:%...
    >
    >>String concatenation, whether you user String.Concat or +=, is
    >>incredibly expensive because it makes a new copy of the string every
    >>time. Try this instead:
    >>
    >> int size= 10048576; // around 10 MB data
    >> StringBuilder buffer = new StringBuilder(size);
    >> for (int j=1; j<=size; j++)
    >> {
    >> buffer.Append("x");
    >> }
    >> Response.Write(buffer.ToString());
    >>
    >>-Jason
    >>
    >>
    >>BlueBall wrote:
    >>
    >>
    >>>I am writing some kind of network testing tool and I have wrote the
    >>>following code in ASP.NET with C#
    >>>
    >>> int size= 10048576; // around 10 MB data
    >>> string buffer = "";
    >>> for (int j=1; j<=1024; j++) {
    >>> buffer = String.Concat(buffer, "x");
    >>> // I tried buffer += "x"; same performance
    >>> }
    >>>
    >>> for (int i=1; i<=(size/1024); i++) {
    >>> Response.Write(buffer);
    >>> // I tried using <asp:literal> same thing
    >>> }
    >>>
    >>>From another computer, I downloaded this file by making that as a link

    >
    > in
    >
    >>>HTML. It was so slow.. I get around 8 Mbps on a 100 Mbps LAN.. In

    >
    > order
    >
    >>>to compare, I tested with a real JPG file I got 25 Mbps! Also, I wrote

    >
    > the
    >
    >>>same thing in PHP using exactly same algorithm. I got around 25 Mbps!
    >>>
    >>>Anyone knows why ASPX performance is slow? is it configruation problem?
    >>>IIS? or is it Response.write not good? I also tried using <asp:

    >
    > literal>
    >
    >>>same thing.. help..
    >>>
    >>>Thanks in advance
    >>>
    >>>
    >>>
    >>>
    >>>

    >
    >
    >
     
    Jason DeFontes, May 13, 2004
    #7
  8. BlueBall

    Hans Kesting Guest

    "BlueBall" <> wrote in message news:eek:2Qoc.62265$...
    > I am writing some kind of network testing tool and I have wrote the
    > following code in ASP.NET with C#
    >
    > int size= 10048576; // around 10 MB data
    > string buffer = "";
    > for (int j=1; j<=1024; j++) {
    > buffer = String.Concat(buffer, "x");
    > // I tried buffer += "x"; same performance
    > }
    >
    > for (int i=1; i<=(size/1024); i++) {
    > Response.Write(buffer);
    > // I tried using <asp:literal> same thing
    > }
    >
    > From another computer, I downloaded this file by making that as a link in
    > HTML. It was so slow.. I get around 8 Mbps on a 100 Mbps LAN.. In order
    > to compare, I tested with a real JPG file I got 25 Mbps! Also, I wrote the
    > same thing in PHP using exactly same algorithm. I got around 25 Mbps!
    >
    > Anyone knows why ASPX performance is slow? is it configruation problem?
    > IIS? or is it Response.write not good? I also tried using <asp: literal>
    > same thing.. help..
    >
    > Thanks in advance
    >
    >
    >
    >
    >


    In addition to the other replies:

    When did you time the performance? The first time an aspx page
    is accessed, it gets compiled to native code. This will have a performance
    hit, but only the first time this page is accessed (in the lifetime
    of the application).
    Neither php or asp have this automatic compile feature so there
    you get no speed dirrerence between the first and second time.


    Hans Kesting
     
    Hans Kesting, May 14, 2004
    #8
  9. 1. You should try to use StringBuilder for concatenation. This will greatly
    improve performance.

    2. You are outputting buffer 9813 times. The problem is that ASP.NET saves
    the output into temporary buffer and only when you done it outputs the
    buffer to browser. The size of the buffer is preset to something big but
    much less than 10Mb. And ASP.NET has to constantly expand the buffer as
    needed when you are outputting it. Which is a big performance hit.
    If you just bluntly set Response.Buffer = false then you going to have
    another problem since ASP.NET will have to use IIS APIs every time you do
    Response.Write which is another performance hit.


    You can minimize it by seting Response.Buffer = true but calling
    Response.Flush every 20th time.

    George.


    "BlueBall" <> wrote in message
    news:eek:2Qoc.62265$...
    > I am writing some kind of network testing tool and I have wrote the
    > following code in ASP.NET with C#
    >
    > int size= 10048576; // around 10 MB data
    > string buffer = "";
    > for (int j=1; j<=1024; j++) {
    > buffer = String.Concat(buffer, "x");
    > // I tried buffer += "x"; same performance
    > }
    >
    > for (int i=1; i<=(size/1024); i++) {
    > Response.Write(buffer);
    > // I tried using <asp:literal> same thing
    > }
    >
    > From another computer, I downloaded this file by making that as a link in
    > HTML. It was so slow.. I get around 8 Mbps on a 100 Mbps LAN.. In order
    > to compare, I tested with a real JPG file I got 25 Mbps! Also, I wrote the
    > same thing in PHP using exactly same algorithm. I got around 25 Mbps!
    >
    > Anyone knows why ASPX performance is slow? is it configruation problem?
    > IIS? or is it Response.write not good? I also tried using <asp: literal>
    > same thing.. help..
    >
    > Thanks in advance
    >
    >
    >
    >
    >
     
    George Ter-Saakov, May 17, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John M
    Replies:
    1
    Views:
    1,311
    Kumar Reddi
    May 29, 2005
  2. Replies:
    3
    Views:
    3,090
  3. HK
    Replies:
    3
    Views:
    481
  4. mike
    Replies:
    3
    Views:
    419
    Virgil Green
    Jul 11, 2005
  5. JosephByrns

    Slow, then quick then slow

    JosephByrns, Jul 10, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    2,519
    codezilla94
    Nov 13, 2007
Loading...

Share This Page