W
wind_2005
Unregistered / Unconfirmed
GUEST, unregistred user!
我在看《Delphi算法与数据结构》(Julian Backnall)一书时,其中作者提到(大意):Delphi5以后,TList及其子类的Clear中使用了Notify方法,使得Delphi5以后的TList运行过慢。。。。
我有其他书中也见到了类似的说法。
《Delphi算法与数据结构》(Julian Backnall)一书中,作者给出了他的算法:
procedure TtdObjectList.Clear;
var
i : integer;
begin
{if we own the items present, free them before clearing the list}
if DataOwner then
for i := 0 to pred(FList.Count) do
TObject(FList).Free;
FList.Clear;
end;
而TList的源码如下:
procedure TList.Clear;
begin
SetCount(0); //见下面
SetCapacity(0);
end;
procedure TList.SetCount(NewCount: Integer);
var
I: Integer;
begin
if (NewCount < 0) or (NewCount > MaxListSize) then
Error(@SListCountError, NewCount);
if NewCount > FCapacity then
SetCapacity(NewCount);
if NewCount > FCount then
FillChar(FList^[FCount], (NewCount - FCount) * SizeOf(Pointer), 0)
else
for I := FCount - 1 downto NewCount do
Delete(I); //见下面
FCount := NewCount;
end;
procedure TList.Delete(Index: Integer);
var
Temp: Pointer;
begin
if (Index < 0) or (Index >= FCount) then
Error(@SListIndexError, Index);
Temp := Items[Index];
Dec(FCount);
if Index < FCount then
System.Move(FList^[Index + 1], FList^[Index],
(FCount - Index) * SizeOf(Pointer));
if Temp <> nil then
Notify(Temp, lnDeleted); //见下面
end;
Notify是虚方法,而其子类TobjectList中实现如下:
procedure TObjectList.Notify(Ptr: Pointer; Action: TListNotification);
begin
if OwnsObjects then
if Action = lnDeleted then
TObject(Ptr).Free;
inherited Notify(Ptr, Action);
end;
===
而我个人感觉(可能是自己水平太次),这和作者的不同之处只是多调了几层涵数,而处理方法是相同的,
那么作者所说的:Notify方法,使得Delphi5以后的TList运行过慢。倒底如何理触,为什么这样说?谢谢!
我有其他书中也见到了类似的说法。
《Delphi算法与数据结构》(Julian Backnall)一书中,作者给出了他的算法:
procedure TtdObjectList.Clear;
var
i : integer;
begin
{if we own the items present, free them before clearing the list}
if DataOwner then
for i := 0 to pred(FList.Count) do
TObject(FList).Free;
FList.Clear;
end;
而TList的源码如下:
procedure TList.Clear;
begin
SetCount(0); //见下面
SetCapacity(0);
end;
procedure TList.SetCount(NewCount: Integer);
var
I: Integer;
begin
if (NewCount < 0) or (NewCount > MaxListSize) then
Error(@SListCountError, NewCount);
if NewCount > FCapacity then
SetCapacity(NewCount);
if NewCount > FCount then
FillChar(FList^[FCount], (NewCount - FCount) * SizeOf(Pointer), 0)
else
for I := FCount - 1 downto NewCount do
Delete(I); //见下面
FCount := NewCount;
end;
procedure TList.Delete(Index: Integer);
var
Temp: Pointer;
begin
if (Index < 0) or (Index >= FCount) then
Error(@SListIndexError, Index);
Temp := Items[Index];
Dec(FCount);
if Index < FCount then
System.Move(FList^[Index + 1], FList^[Index],
(FCount - Index) * SizeOf(Pointer));
if Temp <> nil then
Notify(Temp, lnDeleted); //见下面
end;
Notify是虚方法,而其子类TobjectList中实现如下:
procedure TObjectList.Notify(Ptr: Pointer; Action: TListNotification);
begin
if OwnsObjects then
if Action = lnDeleted then
TObject(Ptr).Free;
inherited Notify(Ptr, Action);
end;
===
而我个人感觉(可能是自己水平太次),这和作者的不同之处只是多调了几层涵数,而处理方法是相同的,
那么作者所说的:Notify方法,使得Delphi5以后的TList运行过慢。倒底如何理触,为什么这样说?谢谢!