创办线程越多,线程中的TIdHttp的Get或Post返回就越慢
创建线程越多,线程中的TIdHttp的Get或Post返回就越慢
uses idhttp;
type
tt=class(TThread)
procedure Execute; override;
end;
procedure tt.Execute;
var
S: string;
x: Cardinal;
begin
inherited;
with TIdHTTP.Create(nil) do
try
Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)';
x := GetTickCount;
S := Get('http://www.baidu.com');{线程创建越多,此处返回越慢}
OutputDebugString(PChar('---------Get耗时:'+IntToStr(GetTickCount-x) + '毫秒'));
finally
Free
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
o: TThread;
begin{只创建一个线程时,idhttp的Get返回最快}
o := tt.Create(False);
o := tt.Create(False);
o := tt.Create(False);
o := tt.Create(False);
o := tt.Create(False);
o := tt.Create(False);
o := tt.Create(False);
o := tt.Create(False);
end;
------解决方案--------------------
这个有什么奇怪的
你带宽为10M 你分成十份线程同时使用 分份线程只能用1M的带宽 差了十倍!
历来 I/O 就是最花时间的
------解决方案--------------------
不奇怪,你网络带宽只有那么多
不过如果只有几个线程就变的很慢,那肯定不正常
------解决方案--------------------
带宽是有限的
------解决方案--------------------
访问不同网站,可能会好一点
因为baidu可能对同一来源的访问加了限制
另外,真的到cpu瓶颈的时候,活动线程数大约cpu、核数,只会更慢
------解决方案--------------------
如果线程多的话,每个线程工作的时间可能比CPU浪费在线程切换的时间还要少
------解决方案--------------------
这么比较是不正确的
LZ代码稍微改下
打印结果
00000000 0.00000000 [9420] 开始GET2988
00000001 0.00177242 [9420] 开始GET8556
00000002 0.00231501 [9420] 开始GET8560
00000003 0.00274778 [9420] 开始GET8396
00000004 0.00312504 [9420] 开始GET9712
00000005 0.00395857 [9420] 开始GET4960
00000006 0.00574477 [9420] 开始GET9776
00000007 0.01052228 [9420] 开始GET7444
00000008 0.02655308 [9420] ---------Get耗时:15毫秒 THreadId:7444
uses idhttp;
type
tt=class(TThread)
procedure Execute; override;
end;
procedure tt.Execute;
var
S: string;
x: Cardinal;
begin
inherited;
with TIdHTTP.Create(nil) do
try
Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)';
x := GetTickCount;
S := Get('http://www.baidu.com');{线程创建越多,此处返回越慢}
OutputDebugString(PChar('---------Get耗时:'+IntToStr(GetTickCount-x) + '毫秒'));
finally
Free
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
o: TThread;
begin{只创建一个线程时,idhttp的Get返回最快}
o := tt.Create(False);
o := tt.Create(False);
o := tt.Create(False);
o := tt.Create(False);
o := tt.Create(False);
o := tt.Create(False);
o := tt.Create(False);
o := tt.Create(False);
end;
------解决方案--------------------
这个有什么奇怪的
你带宽为10M 你分成十份线程同时使用 分份线程只能用1M的带宽 差了十倍!
历来 I/O 就是最花时间的
------解决方案--------------------
不奇怪,你网络带宽只有那么多
不过如果只有几个线程就变的很慢,那肯定不正常
------解决方案--------------------
带宽是有限的
------解决方案--------------------
访问不同网站,可能会好一点
因为baidu可能对同一来源的访问加了限制
另外,真的到cpu瓶颈的时候,活动线程数大约cpu、核数,只会更慢
------解决方案--------------------
如果线程多的话,每个线程工作的时间可能比CPU浪费在线程切换的时间还要少
------解决方案--------------------
这么比较是不正确的
LZ代码稍微改下
with TIdHTTP.Create(nil) do
try
Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)';
x := GetTickCount;
OutputDebugStringA(PChar('开始GET' + IntToStr(GetCurrentThreadId)));
S := Get('http://www.baidu.com');{线程创建越多,此处返回越慢}
OutputDebugString(PChar('---------Get耗时:'+IntToStr(GetTickCount-x) + '毫秒 THreadId:' + IntToStr(GetCurrentThreadId)));
打印结果
00000000 0.00000000 [9420] 开始GET2988
00000001 0.00177242 [9420] 开始GET8556
00000002 0.00231501 [9420] 开始GET8560
00000003 0.00274778 [9420] 开始GET8396
00000004 0.00312504 [9420] 开始GET9712
00000005 0.00395857 [9420] 开始GET4960
00000006 0.00574477 [9420] 开始GET9776
00000007 0.01052228 [9420] 开始GET7444
00000008 0.02655308 [9420] ---------Get耗时:15毫秒 THreadId:7444