在Go lang中,一般类型(int / uint)优于特定类型(int64 / uint64)的优点是什么? [英] What are the advantages of the general types (int / uint) over specific types (int64 / uint64) in Go lang?

查看:129
本文介绍了在Go lang中,一般类型(int / uint)优于特定类型(int64 / uint64)的优点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 int uint 是64位有符号/无符号整数 - 就像 int64 / uint64 。我也明白, int 不是简单的 int64 的别名(例如 byte - > uint8 is),所以 int64 需要转换为 int ,反之亦然。然而,使用其中一个的好处是什么?使用一般类型有没有运行时性能的损失?



对不起,如果这是一个常见问题 - 我已经谷歌搜索答案(并在这里搜索)作为我曾想过其他人可能已经出现过,但没有发现任何人回答这个问题的方式如何影响性能(如果有的话),内存使用情况(我猜他们是不是都是64位整数?)编译:我知道 int / 单元在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)的优点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆