Using ASP to open "multithreading" _asp Foundation

Source: Internet
Author: User

On the Internet to find an ASP opened with the fake thread, found and I have done a program to coincide, but used to be VB, take off, reserve.

1. Principle experimental principle of course all the same, using a Web server to support multiple threads, on the same page to the server to send multiple HTTP requests to complete our work. Or first experiment, in a page at the same time write 2 txt file, compare write time differences. The code is as follows: <%

Startime=timer ()
"----------ASP Implement multithreading----------'"
function Runthread ()
Dim Http
Set Http=server.createobject ("Msxml2.xmlhttp")
Http.open "Get", "http://127.0.0.1/thread.asp?action=b", False
Http.send ()
End Function
function A ()
Dim content,filepath,myfile
Content=now () &AMP;CHR () &timer ()
Filepath=server. MapPath ("A.txt")
Set fso = CreateObject ("Scripting.FileSystemObject")
Set MyFile = fso. CreateTextFile (FilePath, True)
Myfile.write (Content)
Myfile.close
End Function
Function B ()
Dim content,filepath,myfile
Content=now () &AMP;CHR () &timer ()
Filepath=server. MapPath ("B.txt")
Set fso = CreateObject ("Scripting.FileSystemObject")
Set MyFile = fso. CreateTextFile (FilePath, True)
Myfile.write (Content)
Myfile.close
End Function
if (Request.QueryString ("action") = "") Then
Runthread ()
A ()
Else
B ()
End If
%> Script Execution Time:<%=fix (Timer ()-startime) *1000)%>ms The results show that the time in file A and B is basically the same. 2. Practical application Comparison For example, I grabbed 2 pages of HTML code at the same time, a Sohu home page, one is Sina home page, in 2 ways: one is the regular sequence of code execution, single-threaded execution, one is here the multithreaded execution, compare page finish time, code as follows: Testspeed1.asp: <%
Startime=timer ()
function gethttppage (URL)
On Error Resume Next
Dim http
Set Http=server.createobject ("Msxml2.xmlhttp")
Http.open "POST", Url,false
Http.send ()
If Http.readystate<>4 then Exit function
Gethttppage=bytes2bstr (Http.responsebody)
Contents = Gethttppage
Response.Write "<xmp>"
Response.Write (contents)
Response.Write "</xmp>"
Set http=nothing
If Err.number<>0 then err. Clear
End Function
Function Bytes2bstr (vIn)
Dim Strreturn
Dim I,thischarcode,nextcharcode
Strreturn = ""
For i = 1 to LenB (vIn)
Thischarcode = AscB (MidB (vin,i,1))
If Thischarcode < &h80 Then
Strreturn = Strreturn & Chr (Thischarcode)
Else
Nextcharcode = AscB (MidB (vin,i+1,1))
Strreturn = Strreturn & Chr (CLng (thischarcode) * &h100 + CInt (nextcharcode))
i = i + 1
End If
Next
Bytes2bstr = Strreturn
End Function
Gethttppage ("http://www.sohu.com/")
Gethttppage ("http://www.sina.com.cn/")
%> Script Execution Time:<%=fix (Timer ()-startime) *1000)%>ms testspeed2.asp: <%
Startime=timer ()
function gethttppage (URL)
On Error Resume Next
Dim http
Set Http=server.createobject ("Msxml2.xmlhttp")
Http.open "POST", Url,false
Http.send ()
If Http.readystate<>4 then Exit function
Gethttppage=bytes2bstr (Http.responsebody)
Contents = Gethttppage
Response.Write "<xmp>"
Response.Write (contents)
Response.Write "</xmp>"
Set http=nothing
If Err.number<>0 then err. Clear
End Function
Function Bytes2bstr (vIn)
Dim Strreturn
Dim I,thischarcode,nextcharcode
Strreturn = ""
For i = 1 to LenB (vIn)
Thischarcode = AscB (MidB (vin,i,1))
If Thischarcode < &h80 Then
Strreturn = Strreturn & Chr (Thischarcode)
Else
Nextcharcode = AscB (MidB (vin,i+1,1))
Strreturn = Strreturn & Chr (CLng (thischarcode) * &h100 + CInt (nextcharcode))
i = i + 1
End If
Next
Bytes2bstr = Strreturn
End Function
function Runthread ()
Dim Http
Set Http=server.createobject ("Msxml2.xmlhttp")
Http.open "Get", "http://127.0.0.1/thread.asp?action=b", False
Http.send ()
End Function
function A ()
Gethttppage ("http://www.sohu.com/")
End Function
Function B ()
Gethttppage ("http://www.sina.com.cn/")
End Function
if (Request.QueryString ("action") = "") Then
Runthread ()
A ()
Else
B ()
End If
%> Script Execution Time:<%=fix (Timer ()-startime) *1000)%>ms run time result: Times Testspeed1 run time ms Testspeed2.asp run time MS 1 15593 13078 2 13343 14375 3 12828 12515 4 12437 12125 5 12109 11734 6 12281 12140 7 12703 12062 3468 12656 9 12328 12187 10 12343 More 12156 times is another page after one page is finished. Whoever first and then is arbitrary. There is a record exception. To avoid network reasons, the following 5 times to change the test address to the cost machine http://127.0.0.1 11 109 46 12 62 46 13 62 48 14 78 64 15 62 46 above 5 times is one page is finished after another page executes. Whoever first and then is arbitrary. The result: It seems to be a little faster oh ...

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.