C ++风格cast的性能命中? [英] Performance hit from C++ style casts?
问题描述
我是新的C ++风格的演员,我担心使用C ++风格的演员会破坏我的应用程序的性能,因为我有一个 我听说有些演员甚至会扔掉我的中断服务程序中的实时关键的截止日期。异常! 我想使用C ++风格的转换,因为它会使我的代码更健壮。但是,如果有任何性能影响,那么我可能不会使用C ++风格的转换,而是花更多的时间测试使用C风格的转换的代码。 有没有人做过任何严格的测试/性能测试来比较C ++风格的转换和C风格的转换?
<你的结果是什么?
你得出了什么结论?
cast可以概念化的替换为C风格的cast,不会有开销。如果不能,如在 dynamic_cast
的情况下,没有C等效的,你必须以这种方式支付费用。
例如,以下代码:
int x;
float f = 123.456;
x =(int)f;
x = static_cast< int>(f);
使用VC ++生成相同的代码:
00401041 fld dword ptr [ebp-8]
00401044 call __ftol(0040110c)
00401049 mov dword ptr [ebp-4],eax
可以抛出的唯一C ++类型是 dynamic_cast
当投射到参考。为了避免这种情况,转换为指针,如果转换失败,它将返回0。
I am new to C++ style casts and I am worried that using C++ style casts will ruin the performance of my application because I have a real-time-critical deadline in my interrupt-service-routine.
I heard that some casts will even throw exceptions!
I would like to use the C++ style casts because it would make my code more "robust". However, if there is any performance hit then I will probably not use C++ style casts and will instead spend more time testing the code that uses C-style casts.
Has anyone done any rigorous testing/profiling to compare the performance of C++ style casts to C style casts?
What were your results?
What conclusions did you draw?
If the C++ style cast can be conceptualy replaced by a C-style cast there will be no overhead. If it can't, as in the case of dynamic_cast
, for which there is no C equivalent, you have to pay the cost one way or another.
As an example, the following code:
int x;
float f = 123.456;
x = (int) f;
x = static_cast<int>(f);
generates identical code for both casts with VC++ - code is:
00401041 fld dword ptr [ebp-8]
00401044 call __ftol (0040110c)
00401049 mov dword ptr [ebp-4],eax
The only C++ cast that can throw is dynamic_cast
when casting to a reference. To avoid this, cast to a pointer, which will return 0 if the cast fails.
这篇关于C ++风格cast的性能命中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!