处理器最方便的整数类型
据我所知,不是处理器的字大小的整数值在修改它们时将提升(使用+、-、 &等)。 long
类型被认为是字的大小。在我的编译器上,int
是32 位,long
是64 位。
这是否意味着尽管需要更多内存,long
仍比 int
更高效?
另一件事是,复合运算符也能促进价值观吗?那么递增和递减运算符又如何呢?
According to what I've heard, integer values that are not the processor's word size will be promoted when modifying them (using +, -, &, etc). The long
type is supposed to the the word size. On my compiler, int
is 32-bit and long
is 64-bit.
Does this mean long
is more efficient than int
despite requiring more memory?
One more thing, do compound operators also promote the values? And what about the increment and decrement operators?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没关系,这不是你能控制的。选择最窄的类型,其宽度足以代表您需要的值。这是在任何情况下你能做的最好的事情。
该语言保证操作的结果是正确的,并且编译器将选择它能找到的到达该结果的最有效路径。这可能涉及在某个阶段更改整数大小,也可能不涉及。
处理器可以进行其自己的内部转换。再次,不改变结果。再说一次,这不是你能控制的。
It doesn't matter, this isn't something you can control. Choose the narrowest type which is wide enough to represent the values you need. This is the best you can do under any and all circumstances.
The language guarantees the result of operations will be correct, and the compiler will choose the most efficient path to that result that it can find. This may involve changing integer sizes at some stage, or may not.
The processor may do its own internal transformations. Again, without changing the result. Again, it's out of your hands.
任何人都很难给你一个直接的答案。在这种情况下,您能做的最好的事情就是亲自进行分析和查看。
但除非你对这些数字进行数百万次操作,否则我怀疑哪个更快是否重要。首先编写可读的代码,然后进行分析和优化。
我还听说使用
if (int)
比if (bool)
更快,因为bool
会提升为int
(我知道是违反直觉的),但没有人只是为了性能而声明int
而不是bool
。 (除非,也许,在分析之后)It's going to be hard for anyone to give you a straight-forward answer. The best you can do in this case is to profile and see for yourself.
But unless you make millions of operations with the numbers, I doubt it will matter which is faster. Write readable code first, profile, and optimize only after.
I also heard that it's faster to have an
if (int)
rather thanif (bool)
because thebool
would get promoted toint
(counter-intuitive, I know), but no one declares anint
instead of abool
just for the sake of performance. (unless, maybe, after profiling)原则上,普通
int
应该是“快速、正常使用的整数”,而long
当不同时,可以表示“扩展范围,但可能慢点”。实际发生的情况很大程度上取决于您正在使用的平台。
在几个微控制器上,我使用的
int
是 16 位,long
是 32 位,并且long
上的操作需要多个处理器指令。在“经典”32 位 x86 上,long
和int
通常是相同的,因此根本没有区别。在 x86_64 上,根据可移植性考虑,long
可能是 32 位或 64 位;就“执行操作的指令计数”而言,它们是相同的,但是如果您必须在内存中读取/存储大整数数组(32 位整数可能性能更好,因为更适合缓存),则增加的大小可能很重要(并且可能您可以考虑很多因素,优化通常是违反直觉的,尤其在 x86 上)。所以,长话短说:不要过度考虑这个问题,如果您需要一个保证快速工作并且其范围适合您的应用程序的“正常”整数,只需使用
int
即可。如果您需要最小保证大小,请查看
的typedef
(它除了提供精确大小的整数之外,还提供“最快的整数”)具有这个最小尺寸”)。但与往常一样,通常的规则适用:如果遇到性能问题,首先进行分析,然后进行优化。
In line of principle, plain
int
should be the "fast, normal use integer", whilelong
, when different, can mean "extended range but may be slower".What actually happens depends strongly from the platform you are working on.
On several microcontrollers I used
int
is 16 bit andlong
is 32 bit, and operations onlong
take more than one processor instructions. On "classic" 32 bit x86,long
andint
are usually the same, so there's no difference at all. On x86_64, depending on the portability concerns,long
may be 32 or 64 bit; as far as "instruction count to perform an operation" they are the same, but the increased size can matter if you have to read/store big arrays of integers in memory (32 bit integers may perform better because more fit in cache) (and probably there are much many considerations you could do, optimization is often counterintuitive, especially on x86).So, long story short: don't overthink this issue, if you need a "normal" integer that is guaranteed to work fast and its range is ok for your application just use
int
. If you need a minimum guaranteed size, look at thetypedef
s of<stdint.h>
(which, besides giving you exactly-sized integers, provides also "fastest integer with this minimum size").But as always, the usual rule applies: if you have performance problems first profile, then optimize.
这完全取决于您的计算机。没有一种完美的整数类型是普遍最优的。如果它对性能至关重要,您需要使用一些分析工具进行检查。如果它对性能并不重要,那么您就不必关心。
That depends entirely on your computer. There is no one perfect integer type that is universally optimal. If it's critical to performance you'll need to check with some analysis tool. If it isn't critical to performance you shouldn't care.