问题描述
我看到有人建议在高性能应用程序中尽可能避免对变量进行类型转换.我从来不明白这是什么原因,我倾向于多次打字,主要是为了避免编译器警告.这对性能有什么影响吗?常见代码:
I have seen people recommending to avoid typecasting of variables as much as possible in high performing applications. I never understood the reason for this and I tend to typecast lot of times mostly to avoid compiler warning. Does this have any implications on performance?.Commonly seen code:
1) struct X *
传递给一个接受 void *
1) struct X *
pass to a function which takes void *
2)uint16_t
类型转换为 uint32_t
推荐答案
不是直接的.类型转换表示从一种数据类型到另一种数据类型的显式转换.确实是转换有可能对性能产生影响,如果无论您是否进行转换都将执行此类转换,那么转换不会对性能产生任何影响.
Not directly. A type cast expresses an explicit conversion of a value from one data type to another. It is really the conversion that has the potential for a performance impact, and if such a conversion is going to be performed whether you cast or not, then the cast has no performance impact whatever.
例如,某些编译器可以设置为警告从浮点类型到整数类型的隐式转换,并且通常可以通过使用强制转换使转换显式来消除这些警告.这并没有改变将执行转换的事实,并且此类转换不是免费的,但强制转换不会使转换变得比其他情况更昂贵.
For example, some compilers can be set to warn about implicit conversions from floating-point types to integer types, and often these warnings can be silenced by making the conversion explicit with a cast. That does not change the fact that a conversion will be performed, and that such conversions are not free, but the cast doesn't make the conversion any more expensive than it otherwise would be.
另外,一些转换可以免费实现.例如,在大多数机器上,具有相同宽度的有符号和无符号整数类型具有兼容的表示形式,因此在这些类型之间转换值是无操作的.仅添加或删除 _Atomic
以外的类型限定符的强制转换也属于此类别.
Additionally, some conversions can be implemented for free. For example, on most machines, signed and unsigned integer types with of the same width have compatible representations, so converting values between these types is a no-op. Casts that only add or remove type qualifiers other than _Atomic
are also in this category.
关于您的具体示例:
1) struct X *
传递给一个接受 void *
C 不需要不同指针类型的兼容表示,但实际上,如今不同的对象指针类型具有不同表示的情况很少见.因此,指针类型之间的转换通常是免费的.然而,这并不重要,因为您询问的具体情况是无论您是否插入显式强制转换都将执行转换的情况.
C does not require compatible representations for different pointer types, but in practice it is rare these days for different object pointer types to have different representations. Therefore, conversions between pointer types are usually free. That hardly matters, however, because the specific case you ask about is one in which the conversion will be performed whether you insert an explicit cast or not.
2) uint16_t
类型转换为 uint32_t
这可能是免费的,具体取决于具体情况和编译器实现.例如,如果被转换的值已经保存在 32 位寄存器中,那么它就是一个空操作.此外,编译器可能能够在它出现的特定表达式中将其实现为无操作.另请注意,如果 uint32_t
与 unsigned int
相同,这是常见的,那么 C 语义要求在算术表达式的计算,其中许多都属于转换类别,无论您是否进行转换.
This may be free, depending on specific circumstances and compiler implementation. For example, if the value being converted was already being held in a 32-bit register, then it is a no-op. Additionally, the compiler might be able to implement it as a no-op in the specific expression in which it appears. Note also that if uint32_t
is the same as unsigned int
, as is common, then C semantics require this particular conversion to be performed routinely in the evaluation of arithmetic expressions, so many of these fall into the category of conversions that will happen whether you cast or not.
这篇关于C 中类型转换变量的性能开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!