问题描述
我知道单元在32位体系结构上为32位。为了简洁和比较喜欢,我假设这是一个64位Golang环境。
c> int 和 uint 在64位体系结构上仅为64位。在32位体系结构中,它们是32位。一般的答案是,除非您需要某种精度,否则坚持使用与单词大小相同的数据类型当前的体系结构(例如,32位体系结构上的32位)通常效率稍高一些。
I understand that int and uint are 64bit signed / unsigned integers - just like int64 / uint64. And I also understand that int is not simply an alias for int64 (like byte -> uint8 is), so int64 would need to be converted to int and visa versa when applicable. However what are the benefits of using one over the other? Is there any run time performance penalty for using the general types?
Sorry if this is a common question - I had Googled for the answer (and searched on here too) as I'd have thought others might have cropped up before but didn't find anyone answer the question in terms of how they affect performance (if at all), memory usage (I'm guessing not if they're both 64bit integers?) nor how the compiler treats them.
edit: I'm aware that int / unit are 32bit on 32bit architectures. For the sake of brevity and comparing like for like, I was assuming this is a 64bit Golang environment.
int and uint are only 64-bit on 64-bit architectures. On 32-bit architectures they are 32 bits.
The general answer is that, unless you need a certain precision, sticking with datatypes that are the same size as a word on the current architecture (e.g. 32 bits on a 32-bit architecture) is typically slightly more efficient.
这篇关于在Go lang中,一般类型(int / uint)优于特定类型(int64 / uint64)的优点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!