Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 Response.Write() vs <% %>
 New Topic  Topic Locked
 Printer Friendly
Next Page
Author Previous Topic Topic Next Topic
Page: of 2

h20
Starting Member

39 Posts

Posted - 16 July 2001 :  18:04:47  Show Profile
Which one performs better? A while back someoone sommented on this... I cannot remember what the answer was whough.

Id
Junior Member

USA
129 Posts

Posted - 16 July 2001 :  18:08:29  Show Profile  Visit Id's Homepage
with response.Write you can write out multiple lines of code i.e.
<%
response.write "This is a test string that you may" &_
" at some point use"
%>


Where as with <%= %> you can only right out what's on one line, which is really only useful for writing out variables, where as with Response.Write, you can right out strings and the like when needed

Go to Top of Page

Doug G
Support Moderator

USA
6493 Posts

Posted - 16 July 2001 :  18:27:04  Show Profile
<% %> are delimiters that indicate server-side script. Whatever is contained within <% %> tags is processed on the web server.

You could code a page like

<HTML>
<BODY>
This is a <%response.write "test"%>
</BODY>
</HTML>


======
Doug G
======
Go to Top of Page

Dan Martin
Average Member

USA
528 Posts

Posted - 17 July 2001 :  10:59:12  Show Profile  Visit Dan Martin's Homepage  Send Dan Martin an AOL message  Send Dan Martin an ICQ Message  Send Dan Martin a Yahoo! Message
Well, I remember people saying that this is slow:


<% If a = b Then %>
<i>A equals B</i>
This is slower code
<% end If %>


But, <%=something%> is no different than <%Response.Write(something)%>

In fact, if you ever get an error in that line, you will see it replaces the "=" with Response.write.

In the end, the fast computers of today run either at a pretty good clip. I usually code the way that looks the best, and only worry more about memory leaks and such (those are what really kill performance). If you have enough users, perhaps the .004 of a second you save is important.

-Dan
Go to Top of Page

davemaxwell
Access 2000 Support Moderator

USA
3020 Posts

Posted - 17 July 2001 :  12:47:49  Show Profile  Visit davemaxwell's Homepage  Send davemaxwell an AOL message  Send davemaxwell an ICQ Message  Send davemaxwell a Yahoo! Message
quote:

Well, I remember people saying that this is slow:

<% If a = b Then %>
<i>A equals B</i>
This is slower code
<% end If %>


But, <%=something%> is no different than <%Response.Write(something)%>

In fact, if you ever get an error in that line, you will see it replaces the "=" with Response.write.



True this is slower:

<% If a = b Then %>
<i>A equals B</i>
This is slower code
<% end If %>

This is the better way:

<% If a = b Then
Response.Write "<i>A equals B</i>" & vbCrLf & _
"This is slower code"
end If %>


The speed drain comes from switching from ASP to HTML and back again.


Dave Maxwell
--------------
Proud to be a "World Class" Knucklehead
Go to Top of Page

Doug G
Support Moderator

USA
6493 Posts

Posted - 17 July 2001 :  15:48:01  Show Profile
IMHO, in most normal pages you'll never notice the difference in speed between different coding methods. The overhead for context switching isn't all that great.

FWIW, most of the code I write is completely VBS using response.write, hardly any HTML sneaks in. I tend to use subs, functions and classes heavily.



======
Doug G
======
Go to Top of Page

Deleted
deleted

4116 Posts

Posted - 17 July 2001 :  16:43:24  Show Profile
quote:

IMHO, in most normal pages you'll never notice the difference in speed between different coding methods.
======
Doug G
======



A couple of things:
1) If you run the same code in a loop (say 1000 times) it will make a difference.
2) You will not see the html part in a preview window if you use response.write's. Thus, change to response.write's, after the page design is OK.
3) Some editors (like Dereamweaver) will suffer in html code coloring if you mix them. This is exactly the case if you include the inc_functions.asp in a page (this file has a couple of subs without response.write's). I changed them to response.write's to be able to see the coloring in the main document. (Actually this is a request, and if you don't mind, I want to change them while I'm doing the internationalization)

Cheers.

Think Pink
Go to Top of Page

h20
Starting Member

39 Posts

Posted - 17 July 2001 :  17:09:47  Show Profile
Thanks for the replies everyone. Obviously there is some additional server overhead when you go in and out of ASP / html. Reason beeing the IIS has to load the compiler for whatever language you are using... this is especially the case for non-standard ASP languages i.e. PERL.

I would say that if the page was on either the middle or server tier then using the Response object would be the better approach. Client tiered pages that may place the date or somthing of that nature could use the <%=%> approach... just for convenience and aesthetic reasons.

Again, thanks
Scott

Go to Top of Page

Doug G
Support Moderator

USA
6493 Posts

Posted - 17 July 2001 :  20:16:51  Show Profile
quote:
1) If you run the same code in a loop (say 1000 times) it will make a difference.

A loop that executes 1000 times and has inner code that outputs HTML using both response.write and HTML will be a very unique page, I think. :)

As far as loading interpreters for different scripting languages on an asp page, that happens only once per page no matter how many times you switch in & out of <% %> tags. A better performance tip is to make sure you set the default scripting language for pages that don't use the server default, and don't specify the default language on your page if you know it's the default language for the server (i.e., VBScript for nearly all IIS servers).



======
Doug G
======
Go to Top of Page

mrWize
deleted

119 Posts

Posted - 19 July 2001 :  18:54:36  Show Profile
And the most dramatic performence bost is to set buffering to true, either it is set on the server, win2k/IIS 5 has it as default but NT 4/IIS 4.x has buffering of in an default installation.

Test this approach on top ov every .asp page you script:
<%
Option Explicit

Response.Buffer = TRUE
%>

When using Option Explicit YOU have to DIM all variables, and that makes an speed change too

cya,
mrWize

Go to Top of Page

mrWize
deleted

119 Posts

Posted - 19 July 2001 :  18:59:35  Show Profile
This is good:
<%
Response.Write "This is a line " & _
"and here it will continue"
%>

This is Bad:
REsponse.Write "This is a line "
Response.Write "and here it will continue"

<%
asp code
%>
this is a line
and here it will continue
<%
more asp code
%>

AND

Good:
strSQL = "SELECT SOME_FIELD, ANOTHER_FIELD " & _
"FROM SOME_TABLE " & _
"WHERE THIRD_FIELD = " & intSomeVariable & "_
" ORDER BY SOME_FIELD ASC"

Bad
strSQL = "SELECT SOME_FIELD, ANOTHER_FIELD "
strSQL = strSQL & "FROM SOME_TABLE "
strSQL = strSQL & "WHERE THIRD_FIELD = " & intSomeVariable
strSQL = strSQL & " ORDER BY SOME_FIELD ASC"

especially if You build variabels like this in a loop.

cya,
PatrikB

Go to Top of Page

mrWize
deleted

119 Posts

Posted - 19 July 2001 :  19:04:13  Show Profile
Forgot one more thing.

When You have to get anything from the Request object It´s better to get it before You open any connections to a database, especially when using cookies.

i.e:
strCookieValue = Request.Cookies("CookieName")("CookieKey")

then open database and instead of going to the request object You "scream" for the variable, strCookieValue, instead.

cya,
mrWize

Go to Top of Page

Dan Martin
Average Member

USA
528 Posts

Posted - 20 July 2001 :  14:18:25  Show Profile  Visit Dan Martin's Homepage  Send Dan Martin an AOL message  Send Dan Martin an ICQ Message  Send Dan Martin a Yahoo! Message
quote:

Good:
strSQL = "SELECT SOME_FIELD, ANOTHER_FIELD " & _
"FROM SOME_TABLE " & _
"WHERE THIRD_FIELD = " & intSomeVariable & "_
" ORDER BY SOME_FIELD ASC"


If I'm doing my math right, that's 1 RAM access.
quote:

Bad
strSQL = "SELECT SOME_FIELD, ANOTHER_FIELD "
strSQL = strSQL & "FROM SOME_TABLE "
strSQL = strSQL & "WHERE THIRD_FIELD = " & intSomeVariable
strSQL = strSQL & " ORDER BY SOME_FIELD ASC"


That's 4 RAM accesses.

You'd have to loop a LOT of times to notice that at all. Pretty trivial when memory is running at 133,000,000 cycles per second (PC133 memory), or even 266,000,000 for (PC2100 DDR memory).

So, we are talking about 1/133,000,000 of a second for the "Good" method, and 4/133,000,000 for the "Bad" method.

Or change that to 100,000,000 if you purchased the cheap memory.

But, I'm no optimization guru, so maybe I'm missing something.

-Dan

Edited by - Dan Martin on 20 July 2001 14:29:26

Edited by - Dan Martin on 20 July 2001 14:30:06
Go to Top of Page

Doug G
Support Moderator

USA
6493 Posts

Posted - 20 July 2001 :  15:47:27  Show Profile
quote:
That's 4 RAM accesses

It will be a lot, lot more than 1 or 4 ram accesses. Building strings by concatenation is somewhat more computer intensive than just using line extensions. However, it is so common for me to have computations and branches when I need to build a string I usually use the plan b above.

There is an excellent reference for optimizing asp code at this link

http://msdn.microsoft.com/workshop/server/asp/ASPtips.asp

======
Doug G
======
Go to Top of Page

Deleted
deleted

4116 Posts

Posted - 20 July 2001 :  16:07:20  Show Profile
quote:

It will be a lot, lot more than 1 or 4 ram accesses.



Yes much and much. There must be an interpreter behind it, taking all the strings, parsing them into tokens, making calls to OS subroutines, sending messages, waiting for the result, etc. If you guys have taken cources on computer language/compiler design must know that.


Think Pink
Go to Top of Page

Id
Junior Member

USA
129 Posts

Posted - 20 July 2001 :  16:52:53  Show Profile  Visit Id's Homepage
I think what Dan's trying to say, is that on today's typical server machine you're not going to notice a difference between Method A for creating a query and Method B. Now on a benchmark test, there would be a few milliseconds of difference, but to the normal viewer, they won't know any better, and that's what matters

Go to Top of Page
Page: of 2 Previous Topic Topic Next Topic  
Next Page
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.34 seconds. Powered By: Snitz Forums 2000 Version 3.4.07