What are the advantages of the general types (int / uint) over specific types (int64 / uint64) in Go lang?

huangapple go评论77阅读模式
英文:

What are the advantages of the general types (int / uint) over specific types (int64 / uint64) in Go lang?

问题

我理解intuint是64位有符号/无符号整数,就像int64/uint64一样。我也知道int不仅仅是int64的别名(就像byte->uint8一样),所以在适用的情况下,需要将int64转换为int,反之亦然。然而,使用其中一个的好处是什么?使用通用类型是否会有运行时性能损失?

如果这是一个常见问题,我很抱歉-我已经在谷歌上搜索了答案(也在这里搜索了),因为我认为其他人可能会提出这个问题,但没有找到任何人回答这个问题,无论是在性能方面如何(如果有的话),内存使用情况(如果它们都是64位整数,我猜测不会有)还是编译器如何处理它们。

编辑:我知道在32位架构上,int/unit是32位的。为了简洁起见并进行类似比较,我假设这是一个64位的Golang环境。

英文:

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.

答案1

得分: 26

intuint在64位架构上只有64位。在32位架构上它们是32位。

一般来说,除非你需要特定的精度,否则在当前架构上选择与字长相同大小的数据类型(例如在32位架构上选择32位)通常会稍微更高效。

英文:

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.

答案2

得分: 8

intuint对应于Go实现和运行时中基本Go数据结构的最大可能长度。stringmap[K]T[]Tchan T的长度始终适合于int,而[]Tchan T的容量始终适合于int

通过make进行的分配将返回一个长度和容量始终适合于int的对象。内置函数append返回一个长度和容量永远不超过int的切片。在插入新的键值对后,映射的长度(定义键的数量)始终适合于int

主要好处是<i>intuint是与Go程序中的常见Go数据类型(如切片和映射)结合使用时最小的(以位大小为单位)安全数据类型。</i>

int的大小与指针*T的大小无关。与*T对应的整数类型是uintptr。理论上,Go实现可以选择将int映射到int16 - 许多Go程序仍然可以正常工作,但将分配的大小限制为15位可能过于严格,并且会导致运行时恐慌。

<i>在64位体系结构上,Go 1.0的intuint长度为32位,Go 1.1的长度为64位(请参阅Go 1.1发布说明)。此更改将增加64位体系结构上某些Go程序的内存使用量。</i>。

在Go 1.0和32位体系结构下,明确使用int64而不是int可能会使程序变慢,因为:

  • intint64之间的转换

  • 某些CPU指令(如除法)的性能取决于操作数的大小

在64位体系结构上的Go 1.0中,明确使用int64而不是int可能会使程序变快,因为:

  • 如果所有操作数都是64位,编译器可以为地址和偏移计算生成更高效的代码。在计算地址和偏移时混合使用32位和64位操作数会更慢。

在访问[1&lt;&lt;16]T时使用uint16作为索引可以让编译器删除边界检查指令。

英文:

int and uint correspond to the maximum possible length of basic Go data structures in a Go implementation and runtime. The length of string, map[K]T, []T and chan T always fits into an int, and the capacity of []T and chan T always fits into an int.

An allocation via make is bound to return an object with length and capacity that always fit into an int. The built-in function append returns a slice with length and capacity that never exceed an int. The length (the number of defined keys) of a map after inserting a new key-value pair always fits into an int.

The main benefit is that <i>int and uint are the <b>smallest</b> (in terms of bitsize) data types which are safe to use in a Go program in conjunction with common Go data types such as slices and maps.</i>

The size of int is independent from the size of a pointer *T. The integer type corresponding to *T is uintptr. In theory, a Go implementation could choose to map int to int16 - many Go programs would remain to work correctly, but limiting the size of allocations to 15 bits may be too restrictive and would cause runtime panics.

<i>On 64-bit architectures Go 1.0 has int and uint 32 bits long, Go 1.1 64 bits long (see Go 1.1 Release Notes). This change will increase the memory usage of some Go programs on 64-bit architectures.</i>.

Explicitly using int64 instead of int in a Go program can make it slower under Go 1.0 and on 32-bit architectures because:

  • of conversions between int and int64

  • the performance of certain CPU instructions, such as division, depends on the size of operands

Explicitly using int64 instead of int in a Go program can make it faster under Go 1.0 on 64-bit architectures because:

  • The compiler can generate more efficient code for address and offset computation if all operands are 64-bit. Mixing 32-bit and 64-bit operands when computing addresses and offsets is slower.

Using uint16 as an index when accessing [1&lt;&lt;16]T allows the compiler to remove bound checking instructions.

答案3

得分: 5

除了int是“本地”大小的切片和数组索引是int而不是int64或int32。

英文:

In addition to int being of "native" size slice and array indices are int and not int64 or int32.

huangapple
  • 本文由 发表于 2013年5月8日 03:54:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/16427416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定