ASP程序运行速度测试
1
程序运行速度试验结果:
2
1。作相同的分支条件判断:IF 比 SELECT慢。
3
用以下程序测试:
4
<%
5
dim tttt1,ttt2
6
session("ii")=0
7
for sn=0 to 5
8
ttt1=now()
9
for i=0 to 300000
10
if session("ii")=0 then
11
session("ii")=1
12
else
13
if session("ii")=1 then
14
session("ii")=2
15
else
16
if session("ii")=2 then
17
session("ii")=3
18
else
19
session("ii")=0
20
end if
21
end if
22
end if
23
next
24
ttt2=now()
25
tou=ttt2-ttt1
26
Response.Write sn&"、"&tou*24*60*60&"<br>"
27
next
28
29
for sn=0 to 5
30
ttt1=now()
31
for i=0 to 300000
32
select case session("ii")
33
case 0
34
session("ii")=1
35
case 1
36
session("ii")=2
37
case 2
38
session("ii")=3
39
case 3
40
session("ii")=0
41
end select
42
next
43
ttt2=now()
44
tou=ttt2-ttt1
45
Response.Write sn&"、"&tou*24*60*60&"<br>"
46
next
47
48
%>
49
2, 如果把上例中的SESSION对象改为用普通的变量存。速度会快差不多8倍
50
3,进行字符串连接时往中间加入相同多的字符串,基数越大,越慢。
51
通过下面的程序测试:
52
<%
53
dim tttt1,ttt2
54
session("ii")=0
55
for sn=0 to 5
56
ttt1=now()
57
' txt=""
58
for i=0 to 10000
59
txt="a"&txt
60
next
61
ttt2=now()
62
tou=ttt2-ttt1
63
Response.Write sn&"、"&tou*24*60*60&"<br>"
64
next
65
%>
66
进行同样长字节的字符连接时,汉字比英文快4倍,通过下面的程序测试
67
<%
68
69
dim tttt1,ttt2
70
for sn=0 to 5
71
ttt1=now()
72
txt=""
73
for i=0 to 20000
74
txt="人"&txt
75
next
76
ttt2=now()
77
tou=ttt2-ttt1
78
Response.Write sn&"、"&tou*24*60*60&"<br>"
79
next
80
81
txt=""
82
for sn=0 to 5
83
ttt1=now()
84
txt=""
85
for i=0 to 20000
86
txt="aa"&txt
87
next
88
ttt2=now()
89
tou=ttt2-ttt1
90
Response.Write sn&"、"&tou*24*60*60&"<br>"
91
next
92
93
%>
94
用FOR 循环比DO WHILE循环要快得多,用下面的程序测试,虽然FOR循环中要多一个变量,
95
<%
96
dim tttt1,ttt2
97
98
for sn=0 to 5
99
ttt1=now()
100
i=0
101
do while i<=100000
102
i=i+1
103
loop
104
ttt2=now()
105
tou=ttt2-ttt1
106
Response.Write sn&"、"&tou*24*60*60&"<br>"
107
next
108
109
for sn=0 to 5
110
ttt1=now()
111
ii=0
112
for i=0 to 100000
113
ii=ii+1
114
next
115
ttt2=now()
116
tou=ttt2-ttt1
117
Response.Write sn&"、"&tou*24*60*60&"<br>"
118
next
119
%>
120
定义5000个一个字符的SESSION并不比定义5000个有5000个字符串长的SESSION少花很多时间,两者时间差仅为近一倍,用一秒多钟。倒是生成这个5000个字符长的变量花了不少的时间,<%
121
dim tttt1,ttt2
122
c="a"
123
for sn=0 to 5
124
125
session.abandon
126
ttt1=now()
127
for i=0 to 5000
128
session("s"&i)=c
129
next
130
ttt2=now()
131
tou=ttt2-ttt1
132
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"
133
next
134
135
for i=0 to 5000
136
c="a"&c
137
next
138
139
for sn=0 to 5
140
session.abandon
141
ttt1=now()
142
for i=0 to 5000
143
session("s"&i)=c
144
next
145
ttt2=now()
146
tou=ttt2-ttt1
147
Response.Write sn&"、"&tou*24*60*60&":" &session("s"&i-1)&"<br>"
148
next
149
150
151
%>
152
153
154
这段程序从SN=3起就很慢,而前面非常快
155
<!--#include file="filetou.asp"-->
156
<%
157
dim tttt1,ttt2
158
for sn=0 to 5
159
ttt1=now()
160
for i=1 to 20
161
sql ="SELECT 名称 from user where 名称='阿余'"
162
Set rs=Server.CreateObject("ADODB.RecordSet")
163
rs.Open sql,conn,1,3
164
rs("名称")="阿余"
165
rs.update
166
rs.close
167
next
168
ttt2=now()
169
tou=ttt2-ttt1
170
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"
171
next
172
173
174
%>
175
176
177
而这样就快多了。看来建对象很要花些时间,还有,用MOVE 0,1 和 MOVEFIRST 相比速度没有什么差别。
178
<!--#include file="filetou.asp"-->
179
<%
180
sql ="SELECT 名称 from user where 名称='阿余'"
181
Set rs=Server.CreateObject("ADODB.RecordSet")
182
rs.Open sql,conn,1,3
183
dim tttt1,ttt2
184
for sn=0 to 5
185
ttt1=now()
186
for i=1 to 700
187
rs("名称")="阿余"
188
rs.update
189
rs.movefirst
190
next
191
ttt2=now()
192
tou=ttt2-ttt1
193
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"
194
next
195
%>
196
197
而这两种方式相比,后者要慢3倍,可能是后者要重新查询,但比前面的用RS建查询后又去改,改了又关,相比,要快了不知多少。
198
<!--#include file="filetou.asp"-->
199
<%
200
sql ="SELECT 名称 from user where 名称='阿余'"
201
Set rs=Server.CreateObject("ADODB.RecordSet")
202
rs.Open sql,conn,1,3
203
dim tttt1,ttt2
204
205
for sn=0 to 5
206
ttt1=now()
207
for i=1 to 700
208
rs("名称")="阿余"
209
rs.update
210
rs.movefirst
211
next
212
ttt2=now()
213
tou=ttt2-ttt1
214
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"
215
next
216
217
for sn=0 to 5
218
ttt1=now()
219
for i=1 to 700
220
SQL="UPDATE user set 名称='阿余' where 名称='阿余'"
221
conn.execute sql,0,-1
222
next
223
ttt2=now()
224
tou=ttt2-ttt1
225
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"
226
next
227
228
%>
229
230
231
新加一万条记录谁快?第一种方法用31秒,后者直到超时仍未完成。不得已,少掉一个0,1000条是,后者慢一半。
232
<!--#include file="filetou.asp"-->
233
<%
234
sql ="SELECT 名称 from user where id=0"
235
Set rs=Server.CreateObject("ADODB.RecordSet")
236
rs.Open sql,conn,1,3
237
dim tttt1,ttt2
238
239
ttt1=now()
240
for i=1 to 10000
241
rs.addnew
242
rs("名称")="阿余A"
243
rs.update
244
next
245
ttt2=now()
246
tou=ttt2-ttt1
247
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"
248
249
250
ttt1=now()
251
for i=1 to 10000
252
sql=" INSERT INTO user (名称) VALUES('阿余B')"
253
conn.execute sql,0,-1
254
next
255
ttt2=now()
256
tou=ttt2-ttt1
257
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"
258
259
260
%>
261
262
下面的程序结果说明RS新增记录较快,而删除较慢,用CONN新增慢,但删除很快。
263
运行的结果为:
264
、3.00000007264316:
265
、7.99999998416752:
266
、1.99999983888119:
267
、0:
268
后来用RS新增记录5000条,并用CONN删除这5000条, 结果为:
269
、17.000000202097:
270
、1.00000023376197:
271
程序为:
272
<!--#include file="filetou.asp"-->
273
<%
274
dim tttt1,ttt2
275
ttt1=now()
276
sql ="SELECT 名称 from user where id=0"
277
Set rs=Server.CreateObject("ADODB.RecordSet")
278
rs.Open sql,conn,1,3
279
for i=1 to 1000
280
rs.addnew
281
rs("名称")="阿余A"
282
rs.update
283
next
284
ttt2=now()
285
tou=ttt2-ttt1
286
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"
287
288
289
ttt1=now()
290
for i=1 to 1000
291
sql=" INSERT INTO user (名称) VALUES('阿余B')"
292
conn.execute sql,0,-1
293
next
294
ttt2=now()
295
tou=ttt2-ttt1
296
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"
297
298
299
300
301
ttt1=now()
302
sql ="SELECT 名称 from user where 名称='阿余A'"
303
Set rs=Server.CreateObject("ADODB.RecordSet")
304
rs.Open sql,conn,1,3
305
do while not rs.eof
306
rs.delete
307
rs.update
308
rs.move 0,1
309
loop
310
ttt2=now()
311
tou=ttt2-ttt1
312
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"
313
314
315
ttt1=now()
316
sql ="delete from user where 名称='阿余B'"
317
conn.execute sql,0,-1
318
ttt2=now()
319
tou=ttt2-ttt1
320
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"
321
%>
程序运行速度试验结果:2
1。作相同的分支条件判断:IF 比 SELECT慢。3
用以下程序测试:4
<%5
dim tttt1,ttt26
session("ii")=07
for sn=0 to 58
ttt1=now()9
for i=0 to 30000010
if session("ii")=0 then11
session("ii")=112
else13
if session("ii")=1 then14
session("ii")=215
else16
if session("ii")=2 then17
session("ii")=318
else19
session("ii")=020
end if21
end if22
end if23
next24
ttt2=now()25
tou=ttt2-ttt126
Response.Write sn&"、"&tou*24*60*60&"<br>"27
next28

29
for sn=0 to 530
ttt1=now()31
for i=0 to 30000032
select case session("ii")33
case 034
session("ii")=135
case 136
session("ii")=237
case 238
session("ii")=339
case 340
session("ii")=041
end select42
next43
ttt2=now()44
tou=ttt2-ttt145
Response.Write sn&"、"&tou*24*60*60&"<br>"46
next47

48
%>49
2, 如果把上例中的SESSION对象改为用普通的变量存。速度会快差不多8倍50
3,进行字符串连接时往中间加入相同多的字符串,基数越大,越慢。51
通过下面的程序测试:52
<%53
dim tttt1,ttt254
session("ii")=055
for sn=0 to 556
ttt1=now()57
' txt=""58
for i=0 to 1000059
txt="a"&txt60
next61
ttt2=now()62
tou=ttt2-ttt163
Response.Write sn&"、"&tou*24*60*60&"<br>"64
next65
%>66
进行同样长字节的字符连接时,汉字比英文快4倍,通过下面的程序测试67
<%68

69
dim tttt1,ttt270
for sn=0 to 571
ttt1=now()72
txt=""73
for i=0 to 2000074
txt="人"&txt75
next76
ttt2=now()77
tou=ttt2-ttt178
Response.Write sn&"、"&tou*24*60*60&"<br>"79
next80

81
txt=""82
for sn=0 to 583
ttt1=now()84
txt=""85
for i=0 to 2000086
txt="aa"&txt87
next88
ttt2=now()89
tou=ttt2-ttt190
Response.Write sn&"、"&tou*24*60*60&"<br>"91
next92

93
%>94
用FOR 循环比DO WHILE循环要快得多,用下面的程序测试,虽然FOR循环中要多一个变量,95
<%96
dim tttt1,ttt297

98
for sn=0 to 599
ttt1=now()100
i=0101
do while i<=100000102
i=i+1103
loop104
ttt2=now()105
tou=ttt2-ttt1106
Response.Write sn&"、"&tou*24*60*60&"<br>"107
next108

109
for sn=0 to 5110
ttt1=now()111
ii=0112
for i=0 to 100000113
ii=ii+1114
next115
ttt2=now()116
tou=ttt2-ttt1117
Response.Write sn&"、"&tou*24*60*60&"<br>"118
next119
%>120
定义5000个一个字符的SESSION并不比定义5000个有5000个字符串长的SESSION少花很多时间,两者时间差仅为近一倍,用一秒多钟。倒是生成这个5000个字符长的变量花了不少的时间,<%121
dim tttt1,ttt2122
c="a"123
for sn=0 to 5124

125
session.abandon126
ttt1=now()127
for i=0 to 5000128
session("s"&i)=c129
next130
ttt2=now()131
tou=ttt2-ttt1132
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"133
next134

135
for i=0 to 5000136
c="a"&c137
next138

139
for sn=0 to 5140
session.abandon141
ttt1=now()142
for i=0 to 5000143
session("s"&i)=c144
next145
ttt2=now()146
tou=ttt2-ttt1147
Response.Write sn&"、"&tou*24*60*60&":" &session("s"&i-1)&"<br>"148
next149

150

151
%>152

153

154
这段程序从SN=3起就很慢,而前面非常快155
<!--#include file="filetou.asp"-->156
<%157
dim tttt1,ttt2158
for sn=0 to 5159
ttt1=now()160
for i=1 to 20161
sql ="SELECT 名称 from user where 名称='阿余'"162
Set rs=Server.CreateObject("ADODB.RecordSet") 163
rs.Open sql,conn,1,3164
rs("名称")="阿余"165
rs.update166
rs.close167
next168
ttt2=now()169
tou=ttt2-ttt1170
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"171
next172

173

174
%>175

176

177
而这样就快多了。看来建对象很要花些时间,还有,用MOVE 0,1 和 MOVEFIRST 相比速度没有什么差别。178
<!--#include file="filetou.asp"-->179
<%180
sql ="SELECT 名称 from user where 名称='阿余'"181
Set rs=Server.CreateObject("ADODB.RecordSet") 182
rs.Open sql,conn,1,3183
dim tttt1,ttt2 184
for sn=0 to 5185
ttt1=now()186
for i=1 to 700187
rs("名称")="阿余"188
rs.update189
rs.movefirst190
next191
ttt2=now()192
tou=ttt2-ttt1193
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"194
next195
%>196

197
而这两种方式相比,后者要慢3倍,可能是后者要重新查询,但比前面的用RS建查询后又去改,改了又关,相比,要快了不知多少。198
<!--#include file="filetou.asp"-->199
<%200
sql ="SELECT 名称 from user where 名称='阿余'"201
Set rs=Server.CreateObject("ADODB.RecordSet") 202
rs.Open sql,conn,1,3203
dim tttt1,ttt2 204

205
for sn=0 to 5206
ttt1=now()207
for i=1 to 700208
rs("名称")="阿余"209
rs.update210
rs.movefirst211
next212
ttt2=now()213
tou=ttt2-ttt1214
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"215
next216

217
for sn=0 to 5218
ttt1=now()219
for i=1 to 700220
SQL="UPDATE user set 名称='阿余' where 名称='阿余'"221
conn.execute sql,0,-1222
next223
ttt2=now()224
tou=ttt2-ttt1225
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"226
next227

228
%>229

230

231
新加一万条记录谁快?第一种方法用31秒,后者直到超时仍未完成。不得已,少掉一个0,1000条是,后者慢一半。232
<!--#include file="filetou.asp"-->233
<%234
sql ="SELECT 名称 from user where id=0"235
Set rs=Server.CreateObject("ADODB.RecordSet") 236
rs.Open sql,conn,1,3237
dim tttt1,ttt2 238

239
ttt1=now()240
for i=1 to 10000241
rs.addnew242
rs("名称")="阿余A"243
rs.update244
next245
ttt2=now()246
tou=ttt2-ttt1247
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"248

249

250
ttt1=now()251
for i=1 to 10000252
sql=" INSERT INTO user (名称) VALUES('阿余B')"253
conn.execute sql,0,-1254
next255
ttt2=now()256
tou=ttt2-ttt1257
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"258

259

260
%>261

262
下面的程序结果说明RS新增记录较快,而删除较慢,用CONN新增慢,但删除很快。263
运行的结果为:264
、3.00000007264316:265
、7.99999998416752:266
、1.99999983888119:267
、0:268
后来用RS新增记录5000条,并用CONN删除这5000条, 结果为:269
、17.000000202097:270
、1.00000023376197:271
程序为:272
<!--#include file="filetou.asp"-->273
<%274
dim tttt1,ttt2 275
ttt1=now()276
sql ="SELECT 名称 from user where id=0"277
Set rs=Server.CreateObject("ADODB.RecordSet") 278
rs.Open sql,conn,1,3279
for i=1 to 1000280
rs.addnew281
rs("名称")="阿余A"282
rs.update283
next284
ttt2=now()285
tou=ttt2-ttt1286
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"287

288

289
ttt1=now()290
for i=1 to 1000291
sql=" INSERT INTO user (名称) VALUES('阿余B')"292
conn.execute sql,0,-1293
next294
ttt2=now()295
tou=ttt2-ttt1296
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"297

298

299

300

301
ttt1=now()302
sql ="SELECT 名称 from user where 名称='阿余A'"303
Set rs=Server.CreateObject("ADODB.RecordSet") 304
rs.Open sql,conn,1,3305
do while not rs.eof306
rs.delete307
rs.update308
rs.move 0,1309
loop310
ttt2=now()311
tou=ttt2-ttt1312
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"313

314

315
ttt1=now()316
sql ="delete from user where 名称='阿余B'"317
conn.execute sql,0,-1318
ttt2=now()319
tou=ttt2-ttt1320
Response.Write sn&"、"&tou*24*60*60&":"&session("s"&i-1)&"<br>"321
%> posted on 2005-06-11 19:54 ξσ Dicky σξ 阅读(873) 评论(0) 收藏 举报

浙公网安备 33010602011771号