7

所以在研究了很多引擎之后,我一直在为 iphone 构建一个 2d 框架。如您所知,引擎架构的世界是广阔的,所以我一直在尝试尽可能多地应用最佳实践。

我一直在使用:

uint_fast8_t mId;

如果我查找 uint_fast8_t 的定义,我会发现:

/* 7.18.1.3 Fastest-width integer types */
...
typedef uint8_t          uint_fast8_t;

而且我一直在我的代码中使用这些类型 - 我的问题是,使用这些类型是否有性能优势?幕后究竟发生了什么?除了这是数据的正确数据类型(无符号 8 位整数)这一显而易见的事实之外,是否值得在我的代码中使用它?

这是编译器无论如何都会处理的不必要的优化吗?

谢谢。

编辑:没有回应/答案,所以我要赏金!

4

3 回答 3

23

“快速”整数类型被定义为可用的最快整数类型,至少需要所需的位数(在本例中为 8)。

如果您的平台将 uint_fast8_t 定义为 uint8_t,那么速度绝对没有差异。

原因是在不使用其原生字长时,可能存在较慢的架构。例如,我可以找到一个参考,其中 Alpha 处理器 uint_fast_8_t 被定义为“unsigned int”。

于 2010-01-08T17:00:33.970 回答
3

uint_fast8_t 是保证至少 8 位宽的最快整数。根据您的平台,它可能是 8 位或 16 位或 32 位宽。

它不是由编译器本身处理的,它确实使您的程序执行得更快

Here are some resource I found, You might already have seen them http://embeddedgurus.com/stack-overflow/2008/06/efficient-c-tips-1-choosing-the-correct-integer-size/

http://www.mail-archive.com/avr-gcc-list@nongnu.org/msg03149.html

于 2010-01-08T17:22:28.873 回答
-1

The header in mingw64 said the fast types are "Not actually guaranteed to be fastest for all purposes"

/*  7.18.1.3  Fastest minimum-width integer types
 *  Not actually guaranteed to be fastest for all purposes <---------------------
 *  Here we use the exact-width types for 8 and 16-bit ints.
 */
typedef signed char int_fast8_t;
typedef unsigned char uint_fast8_t;
typedef short  int_fast16_t;
typedef unsigned short  uint_fast16_t;
typedef int  int_fast32_t;
typedef unsigned  int  uint_fast32_t;
__MINGW_EXTENSION typedef long long  int_fast64_t;
__MINGW_EXTENSION typedef unsigned long long   uint_fast64_t;

and that still applies to ARM or other architectures, because using a narrow type requires zero extension or sign extension in many situations which is less optimal than a native int.

However that'll benefit in large arrays or in case or slow operations (like division). I'm not sure how slow ARM divisions are but on x86 64-bit division is much slower than 32-bit or 8-bit division

于 2014-01-25T06:20:15.890 回答