简体   繁体   English

dynamic_cast <>的速度有多快

[英]How fast is dynamic_cast<>

... approximately compared to a typical std::string::operator==() ? ...大致与典型的std::string::operator==() I give some more details below, I'm not sure if they are of any relevance. 我在下面提供一些更多细节,我不确定它们是否具有任何相关性。 Answer with complexity or approximation is good enough. 复杂性或近似性的答案足够好。 Thanks! 谢谢!

Details: I will use it inside a for loop over a list to find some specific instances. 详细信息:我将在列表中的for循环中使用它来查找某些特定实例。 I estimate my average level of inheritance to 3.5 classes. 我估计我的平均继承级别为3.5级。 The one I'm looking for has a parent class, a grandparent and above that two "interfaces", ie to abstract classes with a couple of virtual void abc() = 0; 我正在寻找的那个有一个父类,一个祖父母和两个“接口”之上,即用几个virtual void abc() = 0;抽象类virtual void abc() = 0; .

There is no sub-class to the one I'll be looking for. 我正在寻找的那个没有子类。

It depends hugely on your compiler, your particular class hierarchy, the hardware, all sorts of factors. 它在很大程度上取决于您的编译器,您的特定类层次结构,硬件,各种因素。 You really need to measure it directly inside your particular application . 确实需要在特定应用程序中直接测量它 You can use rdtsc or (on Windows) QueryPerformanceCounter to get a relatively high-precision timer for that purpose. 您可以使用rdtsc或(在Windows上) QueryPerformanceCounter来获得相对高精度的计时器。 Be sure to time loops or sleds of several thousand dynamic_cast<>s, because even QPC only has a ¼μs resolution. 确保数千个dynamic_cast <> s的时间循环或雪橇,因为即使QPC只有¼μs的分辨率。

In our app, a dynamic_cast<> costs about 1 microsecond, and a string comparison about 3ns/character. 在我们的应用程序中,dynamic_cast <>的成本约为1微秒,字符串比较约为3ns /字符。

Both dynamic_cast<> and stricmp() are at the top of our profiles, which means the performance cost of using them is significant. dynamic_cast <>和stricmp()都位于我们的配置文件的顶部,这意味着使用它们的性能成本非常高。 (Frankly in our line of work it's unacceptable to have those functions so high on the profile and I've had to go and rewrite a bunch of someone else's code that uses them.) (坦率地说,在我们的工作中,将这些函数放在配置文件中如此之高是不可接受的,我不得不重写一堆别人使用它们的代码。)

The best answer is to measure, my guess would be that dynamic_cast is faster than comparing any but the shortest strings (or perhaps even than short strings). 最好的答案是测量,我的猜测是dynamic_cast比除了最短的字符串(或者甚至比短字符串)更快。

That being said, trying to determine the type of an object is usually a sign of bad design, according to Liskov's substitution principle you should just treat the object the same and have the virtual functions behave the correct way without examining the type from the outside. 话虽这么说,试图确定一个对象的类型通常是设计糟糕的标志,根据Liskov的替换原则,你应该只对待对象并使虚拟函数表现正确,而不从外部检查类型。


Edit: After re-reading your question I'll stick to There is no sub-class to the one I'll be looking for. 编辑:重新阅读你的问题之后我会坚持到那里没有找到一个子课程。 In that case you can use typeid directly, I believe it should be faster than either of your options (although looking for a specific type is still a code smell in my opinion) 在这种情况下,您可以直接使用typeid ,我相信它应该比您的任何一个选项更快(虽然在我看来寻找特定类型仍然是代码味道

#include <iostream>
#include <typeinfo>

struct top {
    virtual ~top() {} 
};

struct left : top { };
struct right : top { };

int main()
{
    left lft;
    top &tp = lft;   
    std::cout << std::boolalpha << (typeid(lft) == typeid(left)) << std::endl; 
    std::cout << std::boolalpha << (typeid(tp) == typeid(left)) << std::endl; 
    std::cout << std::boolalpha << (typeid(tp) == typeid(right)) << std::endl; 
}

Output: 输出:

true 真正
true 真正
false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM