Delphi中的泛型会导致性能瓶颈吗? [英] Do generics in Delphi cause performance bottlenecks?

查看:137
本文介绍了Delphi中的泛型会导致性能瓶颈吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我一直在开发应用程序,并希望有几种类型的集合。我不想为其类型声明并实现一个新的集合类。因此,我考虑使用泛型,但不确定与普通类型实例相比泛型的性能。性能是我要看的主要内容。我的应用程序是时间紧迫的,甚至不建议丢失几百毫秒。

Recently i have been developing an application and wanted to have a collections of several types. I don't want to declare and implement a new collection class for it's type. So, i thought of going with generics, but wasn't sure about the performance of Generics compared to normal typed instances. Performance is the major thing that i am looking at. My application is time critical and even loosing few 100 milliseconds is also not advisable.

我正在使用Delphi XE3

I am using Delphi XE3

例如:

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

TGenericCollection<T: ICollectionItem> = class
  function Add(T) : Integer;
end;

相比

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

TSomeClassList = class
  function Add(item : TSomeClass) : Integer;
end;


推荐答案

没有性能瓶颈


Delphi泛型是编译的。编译器会在 compile 时了解具体的类型,因此将尽力为您提供最好的代码。在运行时检查生成的代码时,泛型代码和非泛型代码之间应该没有区别。

No performance bottleneck

Delphi generics are compiled. The compiler knows the concrete types at compile time and it's going to do it's best to provide you with the best code it can. There should be no differences between generic and non-generic code when inspecting the generated code at run time.

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

There's a good chance to get better code with generics, because you're more likely to use ready-made, efficient, type-safe data structures. When rolling your own you're likely to cut corners because lets be honest, writing sorting algorithms, efficient allocation, etc is hard.

这篇关于Delphi中的泛型会导致性能瓶颈吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆