C++ 什么时候在 `int` 上使用 `short`?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24371077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use `short` over `int`?
提问by dayuloli
There are many questions that asks for difference between the short
and int
integer types in C++, but practically, when do you choose short
over int
?
有很多问题,对于差的要求short
和int
整数C ++类型,但实际上,当你选择short
了int
?
回答by dayuloli
(See Eric's answerfor more detailed explanation)
(有关更详细的解释,请参阅Eric 的回答)
Notes:
笔记:
- Generally,
int
is set to the 'natural size' - the integer form that the hardware handles most efficiently - When using
short
in an array or in arithmetic operations, theshort
integer is converted intoint
, and so this can introduce a hit on the speed in processingshort
integers - Using
short
can conserve memory if it is narrower thanint
, which can be important when using a large array - Your program will use more memory in a 32-bit
int
system compared to a 16-bitint
system
- 通常,
int
设置为“自然大小” - 硬件处理最有效的整数形式 - 当使用
short
以阵列或算术运算中,short
整数被转换成int
,所以这可以在处理上的速度引入命中short
整数 short
如果比 窄int
,使用可以节省内存,这在使用大数组时很重要- 你的程序将在一个32位的使用更多的内存
int
系统相比,16位int
系统
Conclusion:
结论:
- Use
int
unless you conserving memory is critical, or your program uses a lot of memory (e.g. many arrays). In that case, useshort
.
- 使用
int
除非你节省内存是至关重要的,或者你的程序使用了大量的存储器(例如许多阵列)。在这种情况下,请使用short
.
回答by Eric Finn
You choose short
over int
when:
你选择short
了int
当:
Either
任何一个
- You want to decrease the memory footprint of the values you're storing (for instance, if you're targeting a low-memory platform),
- You want to increase performance by increasing either the number of values that can be packed into a single memory page (reducing page faults when accessing your values) and/or in the memory caches (reducing cache misses when accessing values), and profiling has revealed that there are performance gains to be had here,
- Or you are sending data over a network or storing it to disk, and want to decrease your footprint (to take up less disk space or network bandwidth). Although for these cases, you should prefer types which specify exactly the size in bits rather than
int
orshort
, which can vary based on platform (as you want a platform with a 32-bitshort
to be able to read a file written on a platform with a 16-bitshort
). Good candidates are the types defined in stdint.h.
- 您想减少所存储值的内存占用(例如,如果您的目标是低内存平台),
- 您希望通过增加可以打包到单个内存页面(访问值时减少页面错误)和/或内存缓存(访问值时减少缓存未命中)的值数量来提高性能,并且分析显示这里有性能提升,
- 或者您正在通过网络发送数据或将其存储到磁盘,并希望减少占用空间(以占用更少的磁盘空间或网络带宽)。尽管对于这些情况,您应该更喜欢以位而不是
int
或精确指定大小的类型short
,这可能因平台而异(因为您希望具有 32 位的平台short
能够读取在具有16 位short
)。好的候选者是stdint.h 中定义的类型。
And:
和:
- You have a numeric value which does not need to take on any values that can't be stored in a
short
on your target platform (for a 16-bitshort
, this is-32768
-32767
, or0
-65535
for a 16-bitunsigned short
). - Your target platform (or one of you r target platforms) uses less memory for a
short
than for anint
. The standard only guarantees thatshort
is not largerthanint
, so implementations are allowed to have the same size for ashort
and for anint
.
- 您有一个数值,它不需要采用无法存储在
short
目标平台上的任何值(对于 16 位short
,这是-32768
-32767
,或0
-65535
对于 16 位unsigned short
)。 - 您的目标平台(或您的目标平台之一)使用的内存
short
比int
. 唯一标准来保证short
是不大于比int
,因此允许实现具有相同的尺寸要short
和用于int
。
Note:
笔记:
char
s can also be used as arithmetic types. An answer to "When should I use char
instead of short
or int
?"would read very similarly to this one, but with different numbers (-128
-127
for an 8-bit char
, 0
-255
for an 8-bit unsigned char
)
char
s 也可以用作算术类型。回答“我什么时候应该使用char
而不是short
或int
?” 读起来与这个非常相似,但有不同的数字(-128
-127
表示 8 位char
,0
-255
表示 8 位unsigned char
)
In reality, you likely don't actually want to use the short
type specifically. If you want an integer of specific size, there are types defined in <cstdint>
that should be preferred, as, for example, an int16_t
will be 16 bits on every system, whereas you cannot guarantee the size of a short
will be the same across all targets your code will be compiled for.
实际上,您可能实际上并不想short
专门使用该类型。如果您想要一个特定大小的整数,则应该首选定义<cstdint>
其中的类型,例如,int16_t
每个系统上的an将是 16 位,而您不能保证 a 的大小short
在您的代码的所有目标中都相同将被编译。
回答by Thomas Matthews
In general, you don't prefer short
over int
.
一般来说,你不喜欢short
over int
。
The int type is the processor's native word size
Usually, an int
is the processor's word size.
int 类型是处理器的本机字长。
通常,anint
是处理器的字长。
For example, with a 32-bit word size processor, an int
would be 32 bits. The processor is most efficient using 32-bits. Assuming that short
is 16-bit, the processor still fetches 32-bits from memory. So no efficiency here; actually it's longer because the processor may have to shift the bits to be placed in the correct position in a 32-bit word.
例如,对于 32 位字长的处理器,anint
将为 32 位。处理器使用 32 位时效率最高。假设它short
是 16 位,处理器仍然从内存中获取 32 位。所以这里没有效率;实际上它更长,因为处理器可能必须移动位以放置在 32 位字中的正确位置。
Choosing a smaller data typeThere are standardized data types that are bit specific in length, such as uint16_t
. These are preferred to the ambiguous types of char, short,
and int
. These width specific data types are usually used for accessing hardware, or compressing space (such as message protocols).
选择较小的数据类型有一些长度特定于位的标准化数据类型,例如uint16_t
. 这些是优选的暧昧类型char, short,
和int
。这些特定于宽度的数据类型通常用于访问硬件或压缩空间(例如消息协议)。
Choosing a smaller range
The short
data type is based on rangenot bit width. On a 32-bit system, both short
and int
may have the same 32-bit length.
选择一个较小的范围内
的short
数据类型是基于范围没有被比特宽度。在 32 位系统上,short
和int
可能具有相同的 32 位长度。
Once reason for using short
is because the value will never go past a given range. This is usually a fallacy because programs will change and the data type could overflow.
使用一次的原因short
是因为该值永远不会超过给定的范围。这通常是一种谬论,因为程序会改变并且数据类型可能会溢出。
Summary
Presently, I do not use short
anymore. I use uint16_t
when I access 16-bit hardware devices. I use unsigned int
for quantities, including loop indices. I use uint8_t
, uint16_t
and uint32_t
when size matters for data storage. The short
data type is ambiguous for data storage, since it is a minimum. With the advent of stdint
header files, there is no longer any need for short
.
总结
目前,我不再使用short
了。我uint16_t
在访问 16 位硬件设备时使用。我unsigned int
用于数量,包括循环索引。我使用uint8_t
,uint16_t
并uint32_t
在大小事务用于数据存储。的short
数据类型是不明确的用于数据存储,因为它是一个最小值。随着stdint
头文件的出现,不再需要short
.
回答by Zaphod Beeblebrox
If you don't have any specific constraints imposed by your architecture, I would say you can always use int
. The type short
is meant for specific systems where memory is a precious resource.
如果您的架构没有任何特定限制,我会说您始终可以使用int
. 该类型short
适用于内存是宝贵资源的特定系统。