刚开始学asp时,照书里html中包含asp
后来,用response.write 输出html
可现在看了这一篇文章,难道真的要把response.write写成一句(" & _?
你说说?
——-
(转http://www.ccidnet.com/html//tech/web/2000/11/10/58_1298.html)
什么才是提高asp性能的最佳选择(一)
(作者:青苹果工作室编译 2000年11月10日 17:20)
将asp生成的内容写入响应流中最有效的方法是什么?
使用asp的一个最主要原因是在服务器上生成动态内容。所以很明显,我们测试的起点是确定将动态内容发送到响应流中的最适合的方式。在多种选择中,有两个是最基本的:一是使用内联asp标记,另一个是使用response.write 语句。
为测试这些选择,我们创建了一个简单的asp页面,其中定义了一些变量,然后将它们的值插入表格中。虽然这个页面很简单也不是很实用,但它允许我们分离并测试一些单独的问题。
使用asp内联标记
第一个测试包括使用内联asp标记< %= x % >,其中x是一个已赋值的变量。到目前为止,这个方法是最容易执行的,并且它使页面的html部分保持一种易于阅读和维护的格式。
< % 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。
