Search code examples
c++ccompiler-constructionprocessor

Are "char" and "small int" slower than "int"?


Possible Duplicate:
Performance of built-in types : char vs short vs int vs. float vs. double

Hi. Assume, that you have 32-bit processor. Are 8-bit char and 16-bit short int types slower than native 32-bit int? What about using 64-bit long long int?

Are this datatypes supported by hardware by default, or they are all transformed into 32-bit data anyway, by using additional instructions?

In case, that I have to store a small amount of chars, isn't it faster to store them as ints?


Solution

  • On any modern, practical machine, char, int, and long will all be fast (probably equally fast). Whether short is fast or not varies somewhat between cpu architecture and even different cpu models within a single architecture.

    With that said, there's really no good reason to use small types for single variables, regardless of their speed. Their semantics are confusing (due to default promotions to int) and they will not save you significant space (maybe not even any space). The only time I would ever use char, short, int8_t, int16_t, etc. is in arrays or structs that have to match a fixed binary layout of where you'll have so many of them (e.g. pixels or audio samples) that the size of each one actually matters.