RTTI开销在C ++ [英] RTTI Overhead in C++
问题描述
在C ++程序中启用RTTI的内存/性能开销是多少?
任何人都可以在RTTI机制的内部实现和相关的开销之间点亮一些?
我明白如何使用RTTI通过 typeid
和 dynamic_cast
,我想知道的是内部实现细节
What are the memory/performance overheads of enabling RTTI in a C++ program?
Can anyone please throw some light between the internal implementation of RTTI mechanism and the relevant overheads?
I do understand how to use RTTI through typeid
and dynamic_cast
, what I am trying to know is the internal implementation details of how the run time keeps track of this information and how it is an overhead?
推荐答案
启用RTTI通常只带来很小的开销。通常的实现携带指向对象的vtable中的类型信息结构的指针。因为vtable必须被构造,所以额外的时间很小,就像在类中添加另一个虚函数一样。
Enabling RTTI typically brings only a small overhead. The usual implementation carries a pointer to the type information structure in the vtable of an object. Since the vtable must be constructed anyway, the extra time is small - it's like adding another virtual function to the class.
typeid
因此与调用虚函数相当。
dynamic_cast
较慢 - 它需要遍历继承层次结构来执行转换。过于频繁地调用 dynamic_cast
可能会成为性能瓶颈。 '可以'我的意思是它通常不会...
typeid
is therefore comparable to calling a virtual function.
dynamic_cast
is slower - it needs to traverse the inheritance hierarchy to do a cast. Calling dynamic_cast
too frequently can be a performance bottleneck. By 'can' I mean that it usually won't …
可执行文件大小有点膨胀,因为typeinfo结构需要存储在某处。在大多数情况下,它将不相关。
There is a slight bloat in executable size since the typeinfo structures need to be stored somewhere. In most cases it won't be relevant.
这篇关于RTTI开销在C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!