在 C 中什么整数宽度处理得更快?
在 32 位 CPU 上使用 16 位宽度或 32 位宽度整数时,速度有差异吗?或者,64 位架构上的 32 位与 64 位 int 比较?
换句话说,如果我有一些适合 uint16_t 范围的值,如果性能很重要,我应该使用“unsigned int”吗?
Is there difference in speed when I use 16-bit width or 32-bit width integer on 32-bit CPU? Or, 32-bit vs 64-bit int on 64-bit arch?
In other words, if I have some value that fit into uint16_t ranges, should I use "unsigned int" instead if performance is matter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
标头为“至少具有某些指定宽度的最快整数类型”提供了 typedef,这对您的情况可能会有所帮助:The
<stdint.h>
header provides typedef for the "fastest integer types having at least certain specified widths" which may be helpful in your case :除了构建固定布局的二进制结构或大型数据数组(其中大于必要的大小可能会导致大量内存浪费)之外,您永远不应该使用固定大小的整数类型。
我能想到的
uint16_t
或int16_t
唯一好的用途是 16 位音频样本(仍然是音频的主要格式)。否则,只需使用您知道足够大的普通类型。int
始终至少为 16 位,在 POSIX 和 Windows 上至少为 32 位。如果您需要存储对象的数量,请始终使用
size_t
,如果您需要存储文件偏移量,请始终使用off_t
(遗憾的是仅在 POSIX 上可用)。You should never use the fixed-size integer types except for constructing fixed-layout binary structures or large arrays of data where larger-than-necessary size could lead to huge amounts of wasted memory.
The only good use I can think of for
uint16_t
orint16_t
is 16-bit audio samples (still the predominant format for audio). Otherwise just use an ordinary type you know will be sufficiently large.int
is always at least 16-bit, and on POSIX and Windows it's at least 32-bit.If you need to store a count of objects, always use
size_t
, and if you need to store a file offset, always useoff_t
(unfortunately only available on POSIX).通常所有操作都是在机器的本机单词上执行的,因此当使用较小的类型时,您可能会受到较小的惩罚(例如,当需要
int
时,传递一个short int
会使用符号扩展操作码)。但是,如果可以使用 SSE 指令,它们会更快!当然,使用比机器字更大的类型时,惩罚会更大:)
另外,请小心有符号/无符号操作数,在某些情况下它可能会有所不同。
在这里您可以找到更多相关信息。
Usually all operations are performed on machine's native word, so you could have a small penalty when using smaller types (for example, passing a
short int
when anint
is expected would use a sign extension opcode). But, they would be faster if SSE instrcuctions could be used!And, surely, a much larger penalty when using the larger types than the machine word :)
Also, be careful with signed/unsigned operands, in some cases it can make difference.
Here you can find more about it.
这高度依赖于 CPU,并且没有可靠的方法可以在运行之前知道这一点,特别是因为您没有询问特定的 CPU...
通常认为 16 位算术在 64 位计算机上效率低下,而 32 位计算机则认为 16 位算术在 64 位计算机上效率较低。位算术应该执行得更快或与 64 位算术一样快,但正如我所说,您的里程可能会有所不同,尤其是对于未来的 CPU。
如果您事先不知道目标 CPU 并且这是对时间非常敏感的代码,您可能希望以两种方式实现它,让您的软件在启动时运行快速基准测试,然后使用更快的路径。
This is highly CPU dependent and there's no surefire way to know this ahead of runtime, especially because you're not asking about a specific CPU...
16-bit arithmetic is generally believed to be inefficient on 64-bit computers, and 32-bit arithmetic should perform faster or just as fast as 64-bit arithmetic, but like I said, your mileage may vary, especially with future CPUs.
If you don't know the target CPU ahead of time and this is very time-sensitive code, you may want to implement it both ways, have your software run a quick benchmark at startup, then use the path that's faster.
在许多情况下,您提出的问题并不正确,只有当您使用整数数据进行大量计算而不需要太多加载或将它们存储到内存中时,才应该关注这个问题。这只能通过对特定程序进行基准测试来检查。
对于所有其他程序,正确性和可移植性(按顺序)应该更受关注。因此,您应该始终使用 C(也许还有您的平台)为特殊目的而预见的“通用”类型和 typedef。
应该用
size_t
来完成。应该使用 ptrdiff_t 来完成。
库或系统调用通常是
int
。char
或wchar_t
。这些类型通常非常适合 CPU 寄存器,并且编译器知道可以生成高效的代码。所有其他整数类型都有特殊用途,否则应避免使用。特别是,宽度小于
int
的宽度对于算术没有多大用处,因为标准强制将它们提升为int
或在任何类型的操作之前都未签名
。In many cases, the question that you are asking is not the right one and it should only be of concern when you are doing a lot computations with integer data without too much loading or storing them into memory. This can only be checked by benchmarking a particular program.
For all other programs, correctness and portability (in that order) should be of much more concern. Therefore you should always use the "generic" types and
typedef
s that C (and perhaps your platform) foresees for special purposes.should be done with
size_t
.should be done with
ptrdiff_t
.library or system calls usually are
int
.char
orwchar_t
.These are usually types that fit well into CPU registers and for which the compiler knows to generate efficient code. All other integer types are for special purpose and should otherwise be avoided. In particular types that have a width that is less than the one of
int
are not much use for arithmetic, since the standard enforces that they are promoted toint
orunsigned
before any kind of operation.