1.
List.Count := RowCount;
HighRowIndex := RowCount-1;
for j:=0 to HighRowIndex do
begin
List[j] := ChildClass.Create;
end;//for j
比
for j:=0 to HighRowIndex do
begin
List.Add( ChildClass.Create);
end;//for j
要快, 有时95条记录前者不花时间, 而后者要花 0.015s
2.
TBizObjectClass(ChildClass)很慢, 所以不要在循环里面转换:
for j:=0 to HighRowIndex do
begin
List.Add( TBizObjectClass(ChildClass).Create);
end;//for j
应该改成:
ChildClass := TBizObjectClass(ChildClass);
for j:=0 to HighRowIndex do
begin
List.Add( ChildClass.Create);
end;//for j
注意这里的ChildClass 声明为TClass而不是TBizObjectClass.
本文探讨了在Delphi中优化循环效率的方法,通过对比不同的循环实现方式,指出了使用直接赋值创建对象相较于Add方法更为高效的情况,并强调了避免在循环内部进行类类型转换的重要性。
766

被折叠的 条评论
为什么被折叠?



