构建空数组的最佳实践方法

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

Best practices constructing an empty array

问题

我正在为你翻译以下内容:

我想了解在初始化空数组时的最佳实践。

即,arr1、arr2和arr3之间有什么区别吗?

myArr1 := []int{}
myArr2 := make([]int,0)
var myArr3 []int

我知道它们都创建了一个空的[]int数组,但我想知道,这些语法中是否有一种更可取的?个人而言,我认为第一种语法最易读,但这不是重点。一个争议的关键点可能是数组的容量,默认情况下,三种方式的容量是否相同,因为未指定容量。声明未指定容量的数组是否“不好”?我可以假设它会带来一些性能成本,但它到底有多“不好”?

总结:

  1. 创建空数组的三种方式之间有什么区别?
  2. 当未指定数组容量时,默认容量是多少?
  3. 使用未指定容量的数组会带来什么性能成本?
英文:

I'm wondering about best practices when initializing empty arrays.

i.e. Is there any difference here between arr1, arr2, and arr3?

myArr1 := []int{}
myArr2 := make([]int,0)
var myArr3 []int

I know that they make empty []int but I wonder, is one syntax preferable to the others? Personally I find the first to be most readable but that's beside the point here. One key point of contention may be the array capacity, presumably the default capacity is the same between the three as it is unspecified. Is declaring arrays of unspecified capacity "bad"? I can assume it comes with some performance cost but how "bad" is it really?

/tldr:

  1. Is there any difference between the 3 ways to make an empty
    array?
  2. What is the default capacity of an array when unspecified?
  3. What is the performance cost of using arrays with unspecified capacity?

答案1

得分: 8

首先,这是一个切片而不是数组。在Go语言中,数组和切片是非常不同的,数组具有固定的大小,是类型的一部分。一开始我也对此感到困惑 构建空数组的最佳实践方法

  1. 实际上并没有太大区别。这三种方式都是正确的,而且它们之间的差异应该太小而不值得担心。在我的代码中,我通常会根据具体情况选择最简单的方式。
  2. 0
  3. 目前没有任何开销,直到你需要添加一个元素时,需要分配所需的存储空间。
英文:

First, it's a slice not an array. Arrays and slices in Go are very different, arrays have a fixed size that is part of the type. I had trouble with this at first too 构建空数组的最佳实践方法

  1. Not really. Any if the three is correct, and any difference should be too small to worry about. In my own code I generally use whatever is easiest in a particular case.
  2. 0
  3. Nothing, until you need to add an item, then whatever it costs to allocate the storage needed.

答案2

得分: 7

使用未指定容量的数组会有性能成本。当你开始填充切片时,肯定会有成本。如果你知道切片应该增长到多大,你可以从一开始就为底层数组分配足够的容量,而不是每次底层数组填满时重新分配。

这里有一个简单的示例和计时:

package main

import "fmt"

func main() {

    limit := 500 * 1000 * 1000
    mySlice := make([]int, 0, limit) //vs mySlice := make([]int, 0)

    for i := 0; i < limit; i++ {
        mySlice = append(mySlice, i)
    }

    fmt.Println(len(mySlice))
}

在我的机器上运行结果如下:

使用预分配:

real    0m2.129s
user    0m2.073s
sys     0m1.357s

不使用预分配:

real    0m7.673s
user    0m9.095s
sys     0m3.462s
英文:

>What is the performance cost of using arrays with unspecified capacity?

There is certainly a cost when you start populating the slice. If you know how big the slice should grow, you can allocate capacity of the underlying array from the very begging as opposed to reallocating every time the underlying array fills up.

Here is a simple example with timing:

package main

import &quot;fmt&quot;

func main() {

    limit := 500 * 1000 * 1000
    mySlice := make([]int, 0, limit) //vs mySlice := make([]int, 0)

    for i := 0; i &lt; limit; i++ {
        mySlice = append(mySlice, i)
    }

    fmt.Println(len(mySlice))
}

On my machine:

time go run my_file.go

With preallocation:

real	0m2.129s
user	0m2.073s
sys	    0m1.357s

Without preallocation

real	0m7.673s
user	0m9.095s
sys	    0m3.462s

答案3

得分: 2

有三种方法创建一个空数组,它们之间有什么区别吗?

如果“空数组”表示“len(array)==0”,那么答案是否定的,但实际上只有myArr3==niltrue

当未指定时,数组的默认容量是多少?

默认容量将与您指定的长度相同。

使用未指定容量的数组会带来什么性能成本?

没有性能成本。

英文:

> Is there any difference between the 3 ways to make an empty array?

if empty array means len(array)==0, the answer is no, but actually only myArr3==nil is true.

> What is the default capacity of an array when unspecified?

the default capacity will be same with the len you specify.

> What is the performance cost of using arrays with unspecified capacity?

none

huangapple
  • 本文由 发表于 2017年7月26日 11:46:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/45317074.html
匿名

发表评论

匿名网友

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

确定