L
loskiller
Unregistered / Unconfirmed
GUEST, unregistred user!
都说用多线程可以提高使用效率,但我感觉不出来啊,看下面的代码。
线程代码:
Tmythread = class(TThread)
protected
procedure Execute;override;
public
cou:integer;
end;
{ TmyThread }
procedure TmyThread.Execute;
var
i:integer;
s:integer;
begin
FreeOnTerminate:=true;
for i:=1 to cou do
begin
s:=Round((sin(i)));
end;
end;
调用线程代码:
单线程:
procedure TForm1.Button1Click(Sender: TObject);
begin
t1:=Tmythread.Create(true);
t1.cou:=20000000;
t1.OnTerminate:=x1;
memo1.Lines.Add(inttostr(GetTickCount));
t1.Resume;
end;
procedure TForm1.x1(sender: TObject);
begin
memo1.Lines.Add(inttostr(GetTickCount));
end;
多线程:
procedure TForm1.Button2Click(Sender: TObject);
begin
t1:=Tmythread.Create(true);
t1.cou:=6666666;
t1.OnTerminate:=x1;
t2:=Tmythread.Create(true);
t2.cou:=6666667;
t2.OnTerminate:=x1;
t3:=Tmythread.Create(true);
t3.cou:=6666667;
t3.OnTerminate:=x1;
memo1.Lines.Add(inttostr(GetTickCount));
t1.Resume;
t2.Resume;
t3.Resume;
end;
procedure TForm1.x1(sender: TObject);
begin
memo1.Lines.Add(inttostr(GetTickCount));
end;
对比时间差可以发现差不多是一样的,即使是把循环次数加到20亿也是这样,多线程究竟怎么使用,用在什么地方。
线程代码:
Tmythread = class(TThread)
protected
procedure Execute;override;
public
cou:integer;
end;
{ TmyThread }
procedure TmyThread.Execute;
var
i:integer;
s:integer;
begin
FreeOnTerminate:=true;
for i:=1 to cou do
begin
s:=Round((sin(i)));
end;
end;
调用线程代码:
单线程:
procedure TForm1.Button1Click(Sender: TObject);
begin
t1:=Tmythread.Create(true);
t1.cou:=20000000;
t1.OnTerminate:=x1;
memo1.Lines.Add(inttostr(GetTickCount));
t1.Resume;
end;
procedure TForm1.x1(sender: TObject);
begin
memo1.Lines.Add(inttostr(GetTickCount));
end;
多线程:
procedure TForm1.Button2Click(Sender: TObject);
begin
t1:=Tmythread.Create(true);
t1.cou:=6666666;
t1.OnTerminate:=x1;
t2:=Tmythread.Create(true);
t2.cou:=6666667;
t2.OnTerminate:=x1;
t3:=Tmythread.Create(true);
t3.cou:=6666667;
t3.OnTerminate:=x1;
memo1.Lines.Add(inttostr(GetTickCount));
t1.Resume;
t2.Resume;
t3.Resume;
end;
procedure TForm1.x1(sender: TObject);
begin
memo1.Lines.Add(inttostr(GetTickCount));
end;
对比时间差可以发现差不多是一样的,即使是把循环次数加到20亿也是这样,多线程究竟怎么使用,用在什么地方。