开发者

Are "char" and "small int" slower than "int"? [duplicate]

开发者 https://www.devze.com 2023-02-18 16:46 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Performance of built-in types : char vs short vs int vs. float vs. double
This question already has answers here: Closed 11 years ago.

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 开发者_C百科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?


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.


It depends on the operations in the instruction set as well as the compiler.

0

精彩评论

暂无评论...
验证码 换一张
取 消