类型转换是否占用额外的CPU周期 [英] Does typecasting consume extra CPU cycles

查看:197
本文介绍了类型转换是否占用额外的CPU周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在额外的CPU周期类型转换是否在C / C ++的结果?

Does typecasting in C/C++ result in extra CPU cycles?

我的理解是应该在某些情况下ATLEAST消耗额外的CPU周期。就像从float类型转换成整数,其中CPU应要求为float结构转换为整数。

My understanding is that is should consume extra CPU cycles atleast in certain cases. Like typecasting from float to integer where the CPU should require to convert a float structure to integer.

float a=2.0;
int b= (float)a;

我想了解在哪些情况下会/不会消耗额外的CPU周期。

I would like to understand the cases where it would/would not consume extra CPU cycles.

推荐答案

我想说说两种类型之间的转换是我们应该看什么,而不是是否有投与否。例如:

I would like to say that "converting between types" is what we should be looking at, not whether there is a cast or not. For example

 int a = 10;
 float b = a; 

将是相同的:

 int a = 10;
 float b = (float)a;

此也适用于改变类型的大小,例如

This also applies to changing the size of a type, e.g.

 char c = 'a';
 int b = c; 

这将从一个单字节扩展 C INT 尺寸在C意义[使用字节,不是8位的意义],这将有可能增加超出了datamovement本身的额外指令(或额外clockcycle(多个)使用的指令)。

this will "extend c into an int size from a single byte [using byte in the C sense, not 8-bit sense]", which will potentially add an extra instruction (or extra clockcycle(s) to the instruction used) above and beyond the datamovement itself.

请注意,有时这些转换并不明显。在X86-64,一个典型的例子是使用 INT 而不是 unsigned int类型在数组索引。由于指针是64位的,该索引需要被转换为64位。在一个无符号的情况下,这是微不足道的 - 只是使用的64位版本寄存器的值已经是,因为32位的负载运行将填零寄存器的顶部。但是,如果你有一个 INT ,也可能是负面的。因此,编译器将不得不使用的标志这一扩展到64位指令。这通常不是哪里该指数是基于固定环和所有值都是正计算的问题,但如果你调用一个函数,其中目前尚不清楚,如果该参数是正还是负,编译器肯定是要延长值。同样,如果一个函数返回作为索引的值。

Note that sometimes these conversions aren't at all obvious. On x86-64, a typical example is using int instead of unsigned int for indices in arrays. Since pointers are 64-bit, the index needs to be converted to 64-bit. In the case of an unsigned, that's trivial - just use the 64-bit version of the register the value is already in, since a 32-bit load operation will zero-fill the top part of the register. But if you have an int, it could be negative. So the compiler will have to use the "sign extend this to 64 bits" instruction. This is typically not an issue where the index is calculated based on a fixed loop and all values are positive, but if you call a function where it is not clear if the parameter is positive or negative, the compiler will definitely have to extend the value. Likewise if a function returns a value that is used as an index.

然而,任何有能力的编译器不会盲目加指令的东西从自己的类型转换为自身(如果可能的优化是关闭​​的,它可以这样做 - 但很少的优化应该看到,我们是从X型转换为X型,这并不意味着什么,让我们把它拿走)。

However, any reasonably competent compiler will not mindlessly add instructions to convert something from its own type to itself (possibly if optimization is turned off, it may do - but minimal optimization should see that "we're converting from type X to type X, that doesn't mean anything, lets take it away").

因此​​,简而言之,上面的例子中不添加任何额外的惩罚,但也有一定的地方,从一种类型转换到另一个数据确实增加了额外的指令和/或clockcycles到code的情况。

So, in short, the above example is does not add any extra penalty, but there are certainly cases where "converting data from one type to another does add extra instructions and/or clockcycles to the code".

这篇关于类型转换是否占用额外的CPU周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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