通过“int"、“least"允许的编译器优化和“快"非固定宽度类型 C/C++ [英] Compiler optimizations allowed via "int", "least" and "fast" non-fixed width types C/C++

查看:57
本文介绍了通过“int"、“least"允许的编译器优化和“快"非固定宽度类型 C/C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,当大小很重要时,应该使用固定宽度的整数类型.

Clearly, fixed-width integral types should be used when the size is important.

但是,我读过(Insomniac Games 风格指南),当大小不重要时,int"应该优先用于循环计数器/函数参数/返回代码/等 - 给出的理由是固定宽度类型可以排除某些编译器优化.

However, I read (Insomniac Games style guide), that "int" should be preferred for loop counters / function args / return codes / ect when the size isn't important - the rationale given was that fixed-width types can preclude certain compiler optimizations.

现在,我想区分编译器优化"和更适合目标架构的typedef".后者具有全局范围,我的猜测可能影响非常有限,除非编译器能够以某种方式推断由该 typedef 参数化的程序的全局性能.前者具有本地作用域,编译器可以根据本地寄存器压力/使用情况等自由优化使用的字节数和操作.

Now, I'd like to make a distinction between "compiler optimization" and "a more suitable typedef for the target architecture". The latter has global scope, and my guess probably has very limited impact unless the compiler can somehow reason about the global performance of the program parameterized by this typedef. The former has local scope, where the compiler would have the freedom to optimize number of bytes used, and operations, based on local register pressure / usage, among other things.

标准是否允许对非固定宽度类型进行编译器优化"(正如我们定义的那样)?有什么好的例子吗?

Does the standard permit "compiler optimizations" (as we've defined) for non-fixed-width types? Any good examples of this?

如果不是,并且假设 CPU 可以在较小类型上的运行速度至少与较大类型一样快,那么从性能的角度来看,使用根据本地上下文确定大小的固定宽度整数没有什么坏处.至少这提供了缓解注册压力的可能性,而且我认为不可能更糟.

If not, and assuming the CPU can operate on smaller types as least as fast as larger types, then I see no harm, from a performance standpoint, of using fixed-width integers sized according to local context. At least that gives the possibility of relieving register pressure, and I'd argue couldn't be worse.

推荐答案

经验法则是使用 int 的原因是标准将此整数类型定义为CPU(前提是它对于 INT_MININT_MAX 的范围足够宽.这就是最佳性能的来源.

The reason that the rule of thumb is to use an int is that the standard defines this integral type as the natural data type of the CPU (provided that it is sufficiently wide for the range INT_MIN to INT_MAX. That's where the best-performance stems from.

这篇关于通过“int"、“least"允许的编译器优化和“快"非固定宽度类型 C/C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆