c++ - 什么是快速整数?什么定义了一个整数有多快?

标签 c++ performance c++11

this topic提到了以下内容:

The fast type (int_fast#_t) gives you an integer that’s the fastest type with a width of at least # bits (where # = 8, 16, 32, or 64). For example, int_fast32_t will give you the fastest integer type that’s at least 32 bits.

他所说的最快的整数类型是什么意思?什么定义了速度?

我假设并非所有整数都以相同的方式访问,有些整数比其他整数更容易访问,但我需要知道什么可以导致这种访问速度?

我在一个问题中读到:

On some processors, if a variable gets stored in a register which is longer, the compiler may have to add extra code to lop off any extra bits. For example, if uint16_t x; gets stored in a 32-bit register on the ARM7-TDMI, the code x++; may need to be evaluated as x=((x+1)<<16)>>16);. On compilers for that platform, uint_fast16_t would most likely be defined as synonymous with uint32_t to avoid that.

是什么让它更快?在任何一种情况下,32 位都将在寄存器级别循环。

最佳答案

一些 64 位机器对所有东西都使用 64 位,因此在 32 位空间中进行数学计算需要额外的成本(例如,必须模拟无符号溢出)。在这样的机器上,int_fast32_t 可以是 64 位。同样对于一些 32 位机器——int_fast8_t 可能是 32 位。

x86-64 在这里并没有真正受到影响——它保留了 8 位、16 位和 32 位指令,因此编译器可以只说“在这些寄存器上进行 8 位数学运算”,而寄存器的变化并不重要更宽。如果您在商用台式机或服务器上编程,您可能不需要关心“快速”整数类型。

关于c++ - 什么是快速整数?什么定义了一个整数有多快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46446453/

相关文章:

c++ - 英特尔 AVX : Why is there no 256-bits version of dot product for double precision floating point variables?

c++ - 如何避免动态分配小对象?

c++ - 将 unique_ptr 传递给函数对象

C++:C++11 能否移动语义以避免共享所有权情况下的指针?

c++ - VS2012 MSVCR120D.dll 丢失

c++ - CNG:何时使用 BCrypt* 与 NCrypt* 系列函数

java - 为什么 byte 比 int 慢?类实例化

c# - 当 c# 访问 c++ dll 时尝试读取或写入 protected 内存错误

c++ - C++ 中的带通巴特沃斯滤波器实现

c - 在循环中声明变量的级别是否有所不同?