Is there difference in speed when I use 16-bit width or 32-bit width integer on 32-bit CPU? Or, 32-bit vs 64-bit int on 64-bit arch?
In other words, if I have some value that fit into uint16_t ranges, should I use "unsigned int" instead if performance is matter?
The <stdint.h>
header provides typedef for the "fastest integer types having at least certain specified widths" which may be helpful in your case :
Each of the following types designates an integer type that is usually fastest to operate with among all integer types that have at least the specified width.
The typedef name
int_fastN_t
designates the fastest signed integer type with a width of at least N. The typedef nameuint_fastN_t
designates the fastest unsigned integer type with a width of at least N.The following types are required:
int_fast8_t uint_fast8_t int_fast16_t uint_fast16_t int_fast32_t uint_fast32_t int_fast64_t uint_fast64_t
You should never use the fixed-size integer types except for constructing fixed-layout binary structures or large arrays of data where larger-than-necessary size could lead to huge amounts of wasted memory.
The only good use I can think of for uint16_t
or int16_t
is 16-bit audio samples (still the predominant format for audio). Otherwise just use an ordinary type you know will be sufficiently large. int
is always at least 16-bit, and on POSIX and Windows it's at least 32-bit.
If you need to store a count of objects, always use size_t
, and if you need to store a file offset, always use off_t
(unfortunately only available on POSIX).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With