“自然大小”在C ++中真正意味着什么? [英] What does 'Natural Size' really mean in C++?

查看:145
本文介绍了“自然大小”在C ++中真正意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解自然大小是由特定硬件最有效处理的整数宽度。在数组或算术运算中使用 short 时, short 整数必须首先转换为 int

I understand that the 'natural size' is the width of integer that is processed most efficiently by a particular hardware. When using short in an array or in arithmetic operations, the short integer must first be converted into int.

我不是在寻找简单的答案,例如

I am not looking for simple answers such as


32位架构,它的自然大小是32位

If it has a 32-bit architecture, it's natural size is 32-bit

我想了解 这是最高效的, 为什么 必须在对其执行算术运算之前转换 short

I want to understand why this is most efficient, and why a short must be converted before doing arithmetic operations on it.

奖金Q:当对 long 整数进行算术运算时会发生什么?

Bonus Q: What happens when arithmetic operations are conducted on a long integer?

推荐答案


自然大小是特定硬件。

the 'natural size' is the width of integer that is processed most efficiently by a particular hardware.

不是真的。考虑x64架构。从8到64位的任何大小的算术将基本上是相同的速度。那么为什么所有的x64编译器都在32位 int 上结算呢?好吧,因为有很多代码在那里,最初是为32位处理器写的,很多它隐含依赖int是32位。并且考虑到可以表示高达九个百万分之一的值的类型的近无用性,每个整数的额外的四个字节将实际上未被使用。因此,我们已经确定32位int对于这个64位平台是自然的。

Not really. Consider the x64 architecture. Arithmetic on any size from 8 to 64 bits will be essentially the same speed. So why have all x64 compilers settled on a 32-bit int? Well, because there was a lot of code out there which was originally written for 32-bit processors, and a lot of it implicitly relied on ints being 32-bits. And given the near-uselessness of a type which can represent values up to nine quintillion, the extra four bytes per integer would have been virtually unused. So we've decided that 32-bit ints are "natural" for this 64-bit platform.

比较80286架构。寄存器中只有16位。在这样的平台上执行32位整数加法基本上需要将其分成两个16位加法。实际上做任何事情都涉及到分裂,真的 - 和伴随的减速。 80286的自然整数大小绝对不是 32位。

Compare the 80286 architecture. Only 16 bits in a register. Performing 32-bit integer addition on such a platform basically requires splitting it into two 16-bit additions. Doing virtually anything with it involves splitting it up, really-- and an attendant slowdown. The 80286's "natural integer size" is most definitely not 32 bits.

真的,自然来自于处理效率,内存使用和程序员友好性。这不是酸测试。这是架构/编译器设计师的主观判断问题。

So really, "natural" comes down to considerations like processing efficiency, memory usage, and programmer-friendliness. It is not an acid test. It is very much a matter of subjective judgment on the part of the architecture/compiler designer.

这篇关于“自然大小”在C ++中真正意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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