在 C 中什么整数宽度处理得更快?

发布于 2024-10-10 01:20:52 字数 143 浏览 3 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心如狂蝶 2024-10-17 01:20:52

标头为“至少具有某些指定宽度的最快整数类型”提供了 typedef,这对您的情况可能会有所帮助:

以下每种类型都指定
通常是整数类型
其中操作速度最快
整数类型至少具有
指定宽度。

typedef 名称
int_fastN_t 指定最快的
宽度为 at 的有符号整数类型
至少 N。 typedef 名称 uint_fastN_t
指定最快的无符号
宽度至少为的整数类型
N.

需要以下类型:

int_fast8_t uint_fast8_t  
int_fast16_t uint_fast16_t  
int_fast32_t uint_fast32_t  
int_fast64_t uint_fast64_t  

The <stdint.h> header provides typedef for the "fastest integer types having at least certain specified widths" which may be helpful in your case :

Each of the following types designates
an integer type that is usually
fastest to operate with among all
integer types that have at least the
specified width.

The typedef name
int_fastN_t designates the fastest
signed integer type with a width of at
least N. The typedef name uint_fastN_t
designates the fastest unsigned
integer type with a width of at least
N.

The following types are required:

int_fast8_t               uint_fast8_t  
int_fast16_t              uint_fast16_t  
int_fast32_t              uint_fast32_t  
int_fast64_t              uint_fast64_t  
明媚如初 2024-10-17 01:20:52

除了构建固定布局的二进制结构或大型数据数组(其中大于必要的大小可能会导致大量内存浪费)之外,您永远不应该使用固定大小的整数类型。

我能想到的 uint16_tint16_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 or int16_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 use off_t (unfortunately only available on POSIX).

一萌ing 2024-10-17 01:20:52

通常所有操作都是在机器的本机单词上执行的,因此当使用较小的类型时,您可能会受到较小的惩罚(例如,当需要 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 an int 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.

同尘 2024-10-17 01:20:52

这高度依赖于 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.

他是夢罘是命 2024-10-17 01:20:52

在许多情况下,您提出的问题并不正确,只有当您使用整数数据进行大量计算而不需要太多加载或将它们存储到内存中时,才应该关注这个问题。这只能通过对特定程序进行基准测试来检查。

对于所有其他程序,正确性和可移植性(按顺序)应该更受关注。因此,您应该始终使用 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 typedefs that C (and perhaps your platform) foresees for special purposes.

  • Any type of counting and indexing
    should be done with size_t.
  • Differences and relative positions
    should be done with ptrdiff_t.
  • Error and return codes from standard
    library or system calls usually are
    int.
  • Character strings use char or
    wchar_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 to int or unsigned before any kind of operation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文