Mat*_*ner 13 c architecture performance integer stdint
所述stdint.h报头缺少int_fastest_t并uint_fastest_t与对应{,u}int_fastX_t的类型.对于整数类型的宽度无关紧要的情况,如何选择允许处理最大位数且性能损失最小的整数类型?例如,如果使用朴素方法在缓冲区中搜索第一个设置位,则可能会考虑这样的循环:
// return the bit offset of the first 1 bit
size_t find_first_bit_set(void const *const buf)
{
uint_fastest_t const *p = buf; // use the fastest type for comparison to zero
for (; *p == 0; ++p); // inc p while no bits are set
// return offset of first bit set
return (p - buf) * sizeof(*p) * CHAR_BIT + ffsX(*p) - 1;
}
Run Code Online (Sandbox Code Playgroud)
当然,使用char会导致更多的操作int.但是long long可能导致比在int32位系统上使用的开销更昂贵的操作,等等.
我目前的假设是针对主流架构,使用long是最安全的选择:它在32位系统上是32位,在64位系统上是64位.
int_fast8_t在正确的实现中始终是最快的整数类型.永远不会有小于8位的整数类型(因为CHAR_BIT>=8是必需的),并且因为它int_fast8_t是具有至少8位的最快整数类型,所以它是最快的整数类型,周期.