Copy Frequently-Used Data to Script Variables
When accessing COM objects in ASP, you should copy frequently-used object data to script variables. This will cut down on COM method calls, which are relatively expensive compared to accessing script variables. When accessing Collection and Dictionary objects, this technique also cuts down on expensive lookups.
In general, if you are going to access object data more than once, put that data into a script variable. Prime targets for this optimization are Request variables (Form and QueryString variables). For example, your site may pass around a QueryString variable called UserID. Suppose this UserID is referenced a dozen times on a particular page. Instead of calling
Request("UserID")
a dozen times, assign the UserID to a variable at the top of the ASP page. Then use that variable throughout the page. This will save 11 COM method calls.In practice, accessing COM properties or methods can be deceptively expensive. Here is an example, showing some fairly common code (syntactically speaking):
**********************
Foo.bar.blah.baz = Foo.bar.blah.qaz(1) If Foo.bar.blah.zaq = Foo.bar.blah.abc Then ' ...
**************************
- The variable
Foo
is resolved as a global object. - The variable
bar
is resolved as a member ofFoo.
This turns out to be a COM method call. - The variable
blah
is resolved as a member ofFoo.bar
. This, too, turns out to be a COM method call. - The variable
qaz
is resolved as a member offoo.bar.blah
. Yes, this turns out to be a COM method call. - Invoke
Foo.bar.blah.quaz(1)
. One more COM method call. Get the picture? - Do steps 1 through 3 again to resolve
baz
. The system does not know if the call toqaz
changed the object model, so steps 1 through 3 must be done again to resolvebaz
. - Resolve
baz
as a member ofFoo.bar.blah
. Do the property put. - Do steps 1 through 3 again and resolve
zaq
. - Do steps 1 through 3 yet another time and resolve
abc
.
**********************
Set myobj = Foo.bar.blah ' do the resolution of blah ONCE Myobj.baz = myobj.qaz(1) If Myobj.zaq = Myobj.abc Then '...
***********************
With
statement:********************
With Foo.bar.blah .baz = .qaz(1) If .zaq = .abc Then '... ... End With *********************
Avoid Redimensioning Arrays
Try to avoidRedim
arrays. As far as performance is concerned, if you have a machine that is constrained by physical memory size, it's much better to set the initial dimension of the array to its worst-case scenario—or to set the dimension to its optimal case and redim as necessary. This does not mean that you should just go out and allocate a couple of megabytes of memory if you know you aren't going to need it.The code below shows you gratuitous use of
Dim
and Redim
.****************
<% Dim MyArray() Redim MyArray(2) MyArray(0) = "hello" MyArray(1) = "good-bye" MyArray(2) = "farewell" ... ' some other code where you end up needing more space happens, then ... Redim Preserve MyArray(5) MyArray(3) = "more stuff" MyArray(4) = "even more stuff" MyArray(5) = "yet more stuff" %>
*******************
Dim
the array to the correct size initially (in this case, that's 5), than Redim
the array to make it larger. You may waste a little memory (if you don't end up using all of the elements), but the gain will be speed.Use Response Buffering
You can buffer a whole page worth of output by turning on "response buffering." This minimizes the amount of writes to the browser and thus improves overall performance. Each write has a lot of overhead (both in IIS and in the amount of data sent down the wire), so the fewer the writes there are, the better. TCP/IP works much more efficiently when it can send a few large blocks of data than when it has to send many small blocks because of its slow start and Nagling algorithms (used to minimize network congestion).There are two ways of turning response buffering on. First, you can turn on response buffering for an entire application, using the Internet Services Manager. This is the recommended approach and response buffering is turned on by default for new ASP applications in IIS 4.0 and IIS 5.0. Second, on a page-by-page basis, you can enable response buffering by placing the following line of code near the top of the ASP page:
****************
<% Response.Buffer = True %>
**********************
Response.Cookies
collection). In general, it is best to turn response buffering on for an entire Application. This allows you to avoid the above line of code on every page.Response.Flush
One common complaint about response buffering is that users perceive ASP pages as being less responsive (even though the overall response time is improved) because they have to wait for the entire page to be generated before they start to see anything. For long-running pages, you can turn response buffering off by settingResponse.Buffer = False
. However, a better strategy is to utilize the Response.Flush
method. This method flushes all HTML that has been painted by ASP to the browser. For example, after painting 100 rows of a 1,000-row table, ASP can call Response.Flush
to force the painted results to the browser; this allows the user to see the first 100 rows before the remaining rows are ready. This technique can give you the best of both worlds-response buffering combined with the gradual presentation of data to the browser.(Note that in the above example of a 1,000-row table, many browsers won't start painting the table until they see the closing </table> tag. Check your targeted browsers for support. To get around this, try breaking the table into multiple tables with less rows, and call
Response.Flush
after each table. Newer versions of Internet Explorer will paint tables before they are fully downloaded, and will paint especially fast if you specify the table's column widths; this avoids forcing Internet Explorer to calculate the column widths by measuring the width of the contents of every cell.)The other common complaint about response buffering is that it can use a lot of server memory when generating very large pages. Leaving aside the wisdom of generating large pages, this problem can also be addressed with judicious use of
Response.Flush
.Get news, entertainment and everything you care about at Live.com. Check it out!
ليست هناك تعليقات:
إرسال تعليق