Are “char” and “small int” slower than “int”? [duplicate]

左心房为你撑大大i 提交于 2019-12-09 16:31:04

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/5347042/are-char-and-small-int-slower-than-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!