性能dynamic_cast? [英] Performance of dynamic_cast?

查看:390
本文介绍了性能dynamic_cast?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读问题之前:

这个问题不是使用 dynamic_cast 有多有用。它只是其性能。



我最近开发了一个设计,其中 dynamic_cast 被使用了很多。 >
当与同事讨论它时几乎每个人都说, dynamic_cast 不应该使用,因为它的性能不好(这些是同事有不同的背景我在一个庞大的公司工作)



我决定测试这个方法的性能,而不是只是相信他们。



使用了以下代码:

  ptime firstValue(microsec_clock: :当地时间() ); 

ChildObject * castedObject = dynamic_cast< ChildObject *>(parentObject);

ptime secondValue(microsec_clock :: local_time());
time_duration diff = secondValue - firstValue;
std :: cout<< Cast1 lasts:\t<< diff.fractional_seconds()< 微秒 std :: endl;

上述代码使用 boost :: date_time 在Linux上获取可用的值。

我在一次执行中完成了3 dynamic_cast ,测量它们的代码是相同的。 p>

1次执行的结果如下:

Cast1持续时间:74微秒

Cast2持续时间:2微秒

Cast3持续时间:1微秒



第一次施法总是74-111微秒,在同一次执行中的下列施法需要1-3微秒。



最后我的问题:

dynamic_cast 真的很差吗?

到测试结果它不。我的测试代码是否正确?

为什么这么多开发人员认为它是慢的,如果不是?

解决方案

首先,你需要测量的性能,而不仅仅是几个迭代,因为你的结果将主导的定时器的分辨率。尝试使用1百万+,以建立一个有代表性的图片。此外,这个结果是没有意义的,除非你比较它对某些东西,即做相同的,但没有动态转换。



其次,你需要确保编译器不给通过优化同一个指针上的多个动态转换(因此使用循环,但每次使用不同的输入指针)来假结果。



动态转换会更慢,因为它需要访问对象的RTTI(运行时类型信息)表,并检查转换是否有效。然后,为了正确使用它,你需要添加错误处理代码,检查返回的指针是否 NULL 。所有这些都需要周期。



我知道你不想谈论这个,但一个设计,dynamic_cast使用了很多可能是一个指标你做错了什么...


Before reading the question:
This question is not about how useful it is to use dynamic_cast. Its just about its performance.

I've recently developed a design where dynamic_cast is used a lot.
When discussing it with co-workers almost everyone says that dynamic_cast shouldn't be used because of its bad performance (these are co-workers which have different backgrounds and in some cases do not know each other. I'm working in a huge company)

I decided to test the performance of this method instead of just believing them.

The following code was used:

ptime firstValue( microsec_clock::local_time() );

ChildObject* castedObject = dynamic_cast<ChildObject*>(parentObject);

ptime secondValue( microsec_clock::local_time() );
time_duration diff = secondValue - firstValue;
std::cout << "Cast1 lasts:\t" << diff.fractional_seconds() << " microsec" << std::endl;

The above code uses methods from boost::date_time on Linux to get usable values.
I've done 3 dynamic_cast in one execution, the code for measuring them is the same.

The results of 1 execution were the following:
Cast1 lasts: 74 microsec
Cast2 lasts: 2 microsec
Cast3 lasts: 1 microsec

The first cast always took 74-111 microsec, the following casts in the same execution took 1-3 microsec.

So finally my questions:
Is dynamic_cast really performing bad?
According to the testresults its not. Is my testcode correct?
Why do so much developers think that it is slow if it isn't?

解决方案

Firstly, you need to measure the performance over a lot more than just a few iterations, as your results will be dominated by the resolution of the timer. Try e.g. 1 million+, in order to build up a representative picture. Also, this result is meaningless unless you compare it against something, i.e. doing the equivalent but without the dynamic casting.

Secondly, you need to ensure the compiler isn't giving you false results by optimising away multiple dynamic casts on the same pointer (so use a loop, but use a different input pointer each time).

Dynamic casting will be slower, because it needs to access the RTTI (run-time type information) table for the object, and check that the cast is valid. Then, in order to use it properly, you will need to add error-handling code that checks whether the returned pointer is NULL. All of this takes up cycles.

I know you didn't want to talk about this, but "a design where dynamic_cast is used a lot" is probably an indicator that you're doing something wrong...

这篇关于性能dynamic_cast?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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