Delphi中的泛型是否会导致性能瓶颈?

Rah*_*l W 9 delphi generics delphi-xe3

最近我一直在开发一个应用程序,并希望拥有几种类型的集合.我不想为它的类型声明和实现一个新的集合类.因此,我考虑使用泛型,但不确定泛型与普通类型实例相比的性能.性能是我正在关注的主要事情.我的应用程序是时间关键的,甚至减少几百毫秒也是不可取的.

我正在使用Delphi XE3

例如:

ICollectionItem = interface
  function GetID : string;
  property ID : string read GetId;
end;

TGenericCollection<T: ICollectionItem> = class
  function Add(T) : Integer;
end;
Run Code Online (Sandbox Code Playgroud)

相比

TSomeClass = class(TInterfacedObject, ICollectionItem)
  function GetId : string;
end;

TSomeClassList = class
  function Add(item : TSomeClass) : Integer;
end;
Run Code Online (Sandbox Code Playgroud)

Cos*_*und 7

没有性能瓶颈

编译 Delphi泛型.编译器在编译时知道具体类型,并且最好能够为您提供最好的代码.在运行时检查生成的代码时,通用代码和非通用代码之间应该没有区别.

通过泛型获得更好的代码很有可能,因为您更有可能使用现成的,高效的,类型安全的数据结构.当你自己滚动时,你可能会偷工减料,因为老实说,编写排序算法,有效分配等很难.