The difference between int32_t and int_fast32_t

What is the difference between the two? I know that int32_t has exactly 32 bits regardless of the environment, but, as its name says, quickly, how much faster can int_fast32_t compare to int32_t? And if it is much faster, then why?

+6
source share
2 answers

C is defined in terms of an idealized abstract machine. But real-world hardware has behavioral characteristics that are not taken into account by the language standard. Types _fast are type aliases that allow each platform to define types that are β€œconvenient” for hardware.

For example, if you have an array of 8-bit integers and you want to change them individually, it will be quite inefficient for modern desktop computers, since their boot operations usually want to fill the entire processor register, which is either 32 or 64 bits ("machine the word "). Thus, a large amount of downloaded data is wasted, and, more importantly, you cannot parallelize the loading and saving of two adjacent elements of the array, since they live in the same machine word and, therefore, must be sequentially loaded-modified.

_fast types typically have a width as a machine word, if possible. That is, they can be wider than you need, and therefore consume more memory (and therefore harder to cache!), But your hardware can access them faster. However, it all depends on the usage pattern. (For example, an int_fast8_t array is likely to be an array of machine words, and a hard loop modifying such an array can greatly benefit.)

The only way to find out if any value matters is to compare!

+17
source

int32_t is an integer that is 32 bits. This is useful if you want, for example, to create a structure with precise memory allocation.

int_fast32_t is the "fastest" integer for your current processor, which is finally greater than or equal to int32_t . I don’t know if the gain is really for current processors (x86 or ARM)

But I can finally describe a real case: I worked with a 32-bit PowerPC processor. When accessing the inconsistent 16bits int16_t it was inefficient, since it must first rebuild them in one of its 32-bit registers. For non-memory data, since we had no memory limits, it was more efficient to use int_fast16_t (actually it is 32 bits of int).

+8
source

All Articles