< % OPTION EXPLICIT
Dim FirstName
Dim LastName
Dim MiddleInitial
Dim Address
Dim City
Dim State
Dim PhoneNumber
Dim FaxNumber
Dim EMail
Dim BirthDate
FirstName = "John"
MiddleInitial = "Q"
LastName = "Public"
Address = "100 Main Street"
City = "New York"
State = "NY"
PhoneNumber = "1-212-555-1234"
FaxNumber = "1-212-555-1234"
EMail = "john@public.com"
BirthDate = "1/1/1950"
% >
< HTML >
< HEAD >
< TITLE >Response Test< / TITLE >
< /HEAD >
< BODY >
< H1 >Response Test< /H1 >
< TABLE >
< tr >< td >< b >First Name:< /b >< /td >< td >< %= FirstName % >< /td >< /tr >
< tr >< td >< b >Middle Initial:< /b >< /td >< td >< %= MiddleInitial % >< /td >< /tr >
< tr >< td >< b >Last Name:< /b >< /td >< td >< %= LastName % >< /td >< /tr >
< tr >< td >< b >Address:< /b >< /td >< td >< %= Address % >< /td >< /tr >
< tr >< td >< b >City:< /b >< /td >< td >< %= City % >< /td >< /tr >
< tr >< td >< b >State:< /b >< /td >< td >< %= State % >< /td >< /tr >
< tr >< td >< b >Phone Number:< /b >< /td >< td >< %= PhoneNumber % >< /td >< /tr >
< tr >< td >< b >Fax Number:< /b >< /td >< td >< %= FaxNumber % >< /td >< /tr >
< tr >< td >< b >EMail:< /b >< /td >< td >< %= EMail % >< /td >< /tr >
< tr >< td >< b >Birth Date:< /b >< /td >< td >< %= BirthDate % >< /td >< /tr >
< /TABLE >
< /BODY >
< /HTML > /app1/response1.asp的完整代码
以前的最佳(反应速度) = 8.28 msec/page
在HTML的每一行使用Response.Write 语句
许多比较好的学习文档建议避免使用前面的那种方法。其主要理由是,在输出页面和处理页面施加反应时间的过程中,如果web 服务器不得不在发送纯HTML和处理脚本之间进行转换,就会发生一种被称为上下文转换的问题。大部分程序员一听到这里,他们的第一反应就是将原始的HTML的每一行都包装在Response.Write函数中。
…
Response.Write("< html >")
Response.Write("< head >")
Response.Write(" < title >Response Test< /title >")
Response.Write("< /head >")
Response.Write("< body >")
Response.Write("< h1 >Response Test< /h1 >")
Response.Write("< table >")
Response.Write("< tr >< td >< b >First Name:< /b >< /td >< td >" & FirstName & "< /td >< /tr >")
Response.Write("< tr >< td >< b >Middle Initial:< /b >< /td >< td >" & MiddleInitial & "< /td >< /tr >")
… < /app1/response2.asp的片段
以前的最佳(反应速度) = 8.28 msec/page
反应时间 = 8.08 msec/page
差= -0.20 msec (减少 2.4%)
我们可以看到,使用这种方法与使用内联标记的方法相比在性能上获得的收益非常小,这也许是因为页面给服务器装载了一大堆小的函数调用。这种方法最大的缺点是,由于现在HTML都嵌入脚本中,所以脚本代码变得更加冗长,更加难以阅读和维护。
使用包装函数
当我们试图使用Response.Write 语句这种方法时,最令人灰心的发现可能就是Response.Write 函数不能在每行的结尾处放置一个CRLF 。因此,当你从浏览器中阅读源代码时,本来布置得非常好的HTML,现在成了没有结束的一行。我想,你的下一个发现可能会更令你恐怖:在Response 对象中没有其姊妹函数Writeln 。所以,一个很明显的反应就是为Response.Write 函数创建一个包装函数,以便给每一行都附加一个CRLF 。
…
writeCR("< tr >< td >< b >First Name:< /b >< /td >< td >" & FirstName & "< /td >< /tr >")
…
SUB writeCR(str)
Response.Write(str & vbCRLF)
END SUB
/app1/response4.asp的片段
以前的最佳(反应速度)= 8.08 msec/page
反应时间= 10.11 msec/page
差 = +2.03 msec (增加 25.1%) 当然,由于这种方法有效地使函数调用次数加倍,其对性能的影响也很明显,因此要不惜一切代价避免。具有讽刺意味的是CRLF也向反应流中为每行增加了2个字节,而这是浏览器不需要呈现到页面上的。格式化良好的HTML所做的一切就是让你的竞争者更容易阅读你的HTML源代码并理解你的设计。
将连续的Response.Write 连接到一个单独语句中
不考虑我们前面用包装函数进行的测试,下一个合乎逻辑的步骤就是从单独的Response.Write 语句中提取出所有的字符串,将它们连接到一个单独语句中,这样就减少了函数调用的次数,极大地提高了页面的性能。
…
Response.Write("< html >" & _
"< head >" & _
"< title >Response Test< /title >" & _
"< /head >" & _
"< body >" & _
"< h1 >Response Test< /h1 >" & _
"< table >" & _
"< tr >< td >< b >First Name:< /b >< /td >< td >" & FirstName & "< /td >< /tr >" & _
…
"< tr >< td >< b >Birth Date:< /b >< /td >< td >" & BirthDate & "< /td >< /tr >" & _
"< /table >" & _
"< /body >" & _
"< /html >") /app1/response3.asp的片段
以前的最佳(反应速度)= 8.08 msec/page
反应时间 = 7.05 msec/page
差 = -1.03 msec (减少12.7%)
目前,这是最优化的配置。
将连续的Response.Write 连接到一个单独语句中,在每行结尾处增加一个CRLF
考虑到那些要求他们的源代码从浏览器中看要很纯粹的人,我用vbCRLF 常量在前面测试中每行的结尾处插入了一些回车,然后重新运行。
…
Response.Write("< html >" & vbCRLF & _
"< head >" & vbCRLF & _
" < title >Response Test< /title >" & vbCRLF & _
"< /head >" & vbCRLF & _
… /app1/response5.asp的片段
前面的最佳(反应速度)= 7.05 msec/page
反应时间= 7.63 msec/page
差 = +0.58 msec (增加 8.5%)
运行的结果在性能上有一点降低,这也许是由于额外的串联和增加的字符量。
回顾和观测
从前面有关ASP输出的测试中可以得出一些规则:
* 避免内联ASP的过多使用。
* 总是将连续Response.Write 语句连接进一个单独语句内。
* 永远不要在Response.Write 周围使用包装函数来附加CRLF。
* 如果必须格式化HTML输出,直接在Response.Write 语句内附加CRLF。
>
[1] [2] [3] [下一页] [全文显示]
是否应该开启缓冲器? 通过脚本程序启动缓冲器
在ASP脚本的顶部包含Response.Buffer=True ,IIS就会将页面的内容缓存。
< % OPTION EXPLICIT
Response.Buffer = true
Dim FirstName
… /app1/buffer__1.asp的片段
以前的最佳(反应时间)= 7.05 msec/page < % OPTION EXPLICIT
''-------------------------------------------------------------------------------
… 20 lines …
''-------------------------------------------------------------------------------
Dim FirstName
… /app2/comment_1.asp片段
基准= 5.57 msec/page < %@ LANGUAGE=VBSCRIPT % >
< % OPTION EXPLICIT
Dim FirstName
… /app2/language1.asp片段。
基准值= 5.57 msec/page < %@ ENABLESESSIONSTATE = FALSE % >
< % OPTION EXPLICIT
Dim FirstName
… /app2/session_1.asp片段。
基准值= 5.57 msec/page …
CALL writeTable()
SUB writeTable()
Response.Write("< html >" & _
"< head >" & _
…
"< tr >< td >< b >EMail:< /b >< /td >< td >" & EMail & "< /td >< /tr >" & _
"< tr >< td >< b >Birth Date:< /b >< /td >< td >" & BirthDate & "< /td >< /tr >" & _
"< /table >" & _
"< /body >" & _
"< /html >")
END SUB /app2/function1.asp片段
基准值= 5.57 msec/page < % OPTION EXPLICIT
CALL writeTable()
SUB writeTable()
Dim FirstName
…
Dim BirthDate
FirstName = "John"
…
BirthDate = "1/1/1950"
Response.Write("< html >" & _
"< head >" & _
" < title >Response Test< /title >" & _
"< /head >" & _
"< body >" & _
"< h1 >Response Test< /h1 >" & _
"< table >" & _
"< tr >< td >< b >First Name:< /b >< /td >< td >" & FirstName & "< /td >< /tr >" & _
…
"< tr >< td >< b >Birth Date:< /b >< /td >< td >" & BirthDate & "< /td >< /tr >" & _
"< /table >" & _
"< /body >" & _
"< /html >")
END SUB /app2/function2.asp片段
基准值= 5.57 msec/page < % OPTION EXPLICIT
Dim FirstName
…
Dim BirthDate
FirstName = "John"
…
BirthDate = "1/1/1950"
% > < !-- #include file="inc1.asp" -- >
/app2/include_1.asp片段
基准值 = 5.57 msec/page < % OPTION EXPLICIT
Dim FirstName
…
Dim BirthDate
FirstName = "John"
…
BirthDate = "1/1/1950"
CALL writeTable()
% > < !-- #include file="inc2.asp" -- >
/app2/include_2.asp片段
基准值 = 5.57 msec/page < % OPTION EXPLICIT
On Error Resume Next
Dim FirstName
… /app2/error_1.asp片段
基准值 = 5.57 msec/page < %@ TRANSACTION = REQUIRED % >
< % OPTION EXPLICIT
Dim FirstName
… /app2/transact1.asp片段
基准值 = 5.57 msec/page [上一页] [1] [2] [3] [下一页] [全文显示]
结论 本文第一部分的重要之处在于许多小事情的累积。为了强调这个问题,我设置了最后一个测试,在其中进行了我们以前曾经测试过的看来无所谓但实际上有坏影响的所有操作。我包含了许多Response.Write 声明、关闭了缓冲器、设置了默认语言、去掉了Option Explicit 引用并初始化了错误句柄。
< %@ LANGUAGE=VBSCRIPT % >
< %
On Error Resume Next
FirstName = "John"
…
BirthDate = "1/1/1950"
Response.Write("< html >")
Response.Write("< head >")
Response.Write(" < title >Response Test< /title >")
Response.Write("< /head >")
Response.Write("< body >")
Response.Write("< h1 >Response Test< /h1 >")
Response.Write("< table >")
Response.Write("< tr >< td >< b >First Name:< /b >< /td >< td >" &_
"FirstName & "< /td >< /tr >")
…
Response.Write("< tr >< td >< b >Birth Date:< /b >< /td >< td >" &_
" BirthDate & "< /td >< /tr >")
Response.Write("< /table >")
Response.Write("< /body >")
Response.Write("< /html >")
% > /app2/final_1.asp片段
基准值 = 5.57 msec/page