ASPX Performance - Slow?

B

BlueBall

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
 
C

Chris Torgerson

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
 
J

Jason DeFontes

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
 
B

BlueBall

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.
 
E

Ed Courtenay

BlueBall said:
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.
 
B

BlueBall

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 said:
BlueBall said:
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
 
J

Jason DeFontes

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
 
H

Hans Kesting

BlueBall said:
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
 
G

George Ter-Saakov

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top