Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what integer width processed faster in C?

Tags:

c

integer

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?

like image 461
S.J. Avatar asked Dec 31 '10 16:12

S.J.


2 Answers

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 name uint_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  
like image 97
icecrime Avatar answered Oct 01 '22 07:10

icecrime


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).

like image 32
R.. GitHub STOP HELPING ICE Avatar answered Oct 01 '22 08:10

R.. GitHub STOP HELPING ICE