Improved string handling performance in ASP applications (2)

Source: Internet
Author: User

The basic principle used in this class is to use a variable (m_stext) as a string buffer at the class level and to use the space$ function to populate the buffer with a space character to set it to a specific size. If you want to connect more text to existing text, use the mid$ function to insert text in the correct position after checking the buffer size enough to hold the new text. The ToString function returns the text currently stored in the buffer and resizes the buffer to fit the correct length of the text. The ASP code that uses StringBuilder looks like this:

Function WriteHTML( Data )
Dim oSB
Dim nRep
Set oSB = Server.CreateObject( "StringBuilderVB.StringBuilder" )
' 用大小和增长系数初始化缓冲区
oSB.Init 15000, 7500
For nRep = 0 to 99
oSB.Append "<TR><TD>", (nRep + 1), "</TD><TD> ", _
Data( 0, nRep ), "</TD><TD>", _
Data( 1, nRep ), "</TD><TD>", _
Data( 2, nRep ), "</TD><TD>", _
Data( 3, nRep ), "</TD><TD>", _
Data( 4, nRep ), "</TD><TD>", _
Data( 5, nRep ), "</TD></TR>"
Next
WriteHTML = oSB.ToString()
Set oSB = Nothing
End Function

Using StringBuilder requires a certain amount of overhead because it must be created every time this class is used, and the DLL containing this class must be loaded when the first class instance is created. Overhead is also required for additional method calls to the StringBuilder instance. When using the parenthetical & method, how StringBuilder performs depends on several factors, including the number of connections, the size of the string to build, and the performance of the initialization parameters of the selected StringBuilder string buffer. Note that in most cases, the amount of space required in the buffer is estimated to be slightly higher than it is to keep growing.

Built-in methods

ASP contains a very quick way to create HTML code, just call Response.Write multiple times. The Write function uses an implicitly optimized string buffer that provides very good performance characteristics. The modified writehtml code looks like this:

Function WriteHTML( Data )
Dim nRep
For nRep = 0 to 99
Response.Write "<TR><TD>"
Response.Write (nRep + 1)
Response.Write "</TD><TD>"
Response.Write Data( 0, nRep )
Response.Write "</TD><TD>"
Response.Write Data( 1, nRep )
Response.Write "</TD><TD>"
Response.Write Data( 2, nRep )
Response.Write "</TD><TD>"
Response.Write Data( 3, nRep )
Response.Write "</TD><TD>"
Response.Write Data( 4, nRep )
Response.Write "</TD><TD>"
Response.Write Data( 5, nRep )
Response.Write "</TD></TR>"
Next
End Function

While this code is likely to provide us with the best performance and scalability, the encapsulation has been compromised to some extent because the code inside the function is now written directly to the Response stream, so the calling code loses a certain amount of control. In addition, moving this code (for example, moving to a COM component) becomes more difficult because this function has a dependency on the Response stream.

Test

The four methods mentioned above are tested by a simple ASP page that contains a single table that provides data from a virtual string array. We use Application Center Test? (ACT) from a single client (Windows? XP PROFESSIONAL,PIII-850MHZ,512MB RAM) for a single server in a 100mb/sec network (Windows Advanced server, dual PIII-1000MHZ,256MB RAM) to perform the test. ACT is configured to use 5 threads to simulate the load of 5 users when they connect to a Web site. Each test includes a 20-second warm-up time and a subsequent 100-second load time, creating as many requests as possible during the load.

By changing the number of repetitions in the main table loop, run the test repeatedly for a different number of connection operations, as shown in the code snippet in the WriteHTML function. Each test that runs is performed using the four different methods mentioned above.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.