dtzd65908
2017-07-26 03:46
浏览 1.7k
已采纳

在GoLang中构造空数组的最佳实践?

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?

图片转代码服务由CSDN问答提供 功能建议

我想知道初始化空数组时的最佳实践。

\ n

ie 这里的arr1,arr2和arr3有什么区别吗?

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

我知道他们将空的 [] int 设为空,但是我想知道,哪种语法更可取 给其他人? 就我个人而言,我发现第一个最易读,但这与本文无关。 争用的一个关键点可能是阵列容量,这三个之间的默认容量可能是相同的,因为未指定。 声明未指定容量的数组是否“不好”? 我可以假设它会带来一些性能成本,但是它到底有多“糟糕”?

/ tldr

  1. 制作空 数组的三种方法之间是否有区别?
  2. 未指定时阵列的默认容量是多少?
  3. 使用未指定容量的阵列的性能成本是多少?
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanqiao8925 2017-07-26 04:03
    已采纳

    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.
    打赏 评论
  • dousha2020 2017-07-26 05:34

    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

    打赏 评论
  • dpevsxjn809817 2017-07-26 15:29

    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 "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))
    }
    

    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
    
    打赏 评论

相关推荐 更多相似问题