`short int`vs`int` [英] `short int` vs `int`
问题描述
我应该使用 short int
而不是 int
吗?有什么有用的区别吗?任何陷阱?
Should I bother using short int
instead of int
? Is there any useful difference? Any pitfalls?
推荐答案
Ben说。你实际上会创建效率较低的代码,因为每当进行任何比较时,所有寄存器都需要去掉高位。除非你需要节省内存,因为你有吨,使用本机整数大小。这是 int
的用法。
What Ben said. You will actually create less efficient code since all the registers need to strip out the upper bits whenever any comparisons are done. Unless you need to save memory because you have tons of them, use the native integer size. That's what int
is for.
编辑:甚至没有看到你的关于const的子问题。在内部类型(int,float)上使用const是没用的,但是任何指针/引用在适用的时候应该是const。类方法也一样。
Didn't even see your sub-question about const. Using const on intrinsic types (int, float) is useless, but any pointers/references should absolutely be const whenever applicable. Same for class methods as well.
这篇关于`short int`vs`int`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!