FAQ
Hi fellow gophers,

Between the following two snippets, which one might be more efficient?

sfrom := []int{1, 2, 3}

sto1 := make([]int, len(sfrom))
for i, v := range sfrom {
     sto1[i] = v
}

sto2 := make([]int, 0, len(sfrom))
for _, v := range sfrom {
     sto2 = append(sto2, v)
}

Thanks,
Jackie

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Jan Mercl at May 24, 2013 at 1:34 pm
    On Fri, May 24, 2013 at 3:28 PM, Jackie Li wrote:

    dst := append([]T(nil), src...)

    or

    dst := make([]T, len(src))
    copy(dst, src)

    -j

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Ethan Burns at May 24, 2013 at 1:38 pm
    Not sure if one is more efficient than the other. If you are just copying
    a slice then I would recommend using the copy variant, because it has the
    best combination of clarity and brevity.


    Best,
    Ethan
    On Friday, May 24, 2013 9:33:53 AM UTC-4, Jan Mercl wrote:

    On Fri, May 24, 2013 at 3:28 PM, Jackie Li wrote:

    dst := append([]T(nil), src...)

    or

    dst := make([]T, len(src))
    copy(dst, src)

    -j
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jackie Li at May 24, 2013 at 2:02 pm
    Hi Ethan, Jan,

    Thanks for the reply, my example didn't show my intention, my src is from
    calculated result. it should be:

    sfrom := []int{1, 2, 3}

    sto1 := make([]int, len(sfrom))
    for i, v := range sfrom {
         sto1[i] = rand.Intn(v)
    }

    sto2 := make([]int, 0, len(sfrom))
    for _, v := range sfrom {
         sto2 = append(sto2, rand.Intn(v)
    }


    I tried use -gcflags "-S" to produce the assembly, seems version 1
    generated less lines:
    http://play.golang.org/p/ws3wwD7u2b

    --- prog list "copy1" ---
    0000 (C:\Users\hb19623\Gocode\src\hello\slice.go:5) TEXT
      copy1+0(SB),$48-0
    0001 (C:\Users\hb19623\Gocode\src\hello\slice.go:5) LOCALS ,$0
    0002 (C:\Users\hb19623\Gocode\src\hello\slice.go:6) MOVQ sfrom+8(SB),AX
    0003 (C:\Users\hb19623\Gocode\src\hello\slice.go:6) MOVQ
      $type.[]int+0(SB),(SP)
    0004 (C:\Users\hb19623\Gocode\src\hello\slice.go:6) MOVQ AX,8(SP)
    0005 (C:\Users\hb19623\Gocode\src\hello\slice.go:6) MOVQ AX,16(SP)
    0006 (C:\Users\hb19623\Gocode\src\hello\slice.go:6) CALL
      ,runtime.makeslice+0(SB)
    0007 (C:\Users\hb19623\Gocode\src\hello\slice.go:6) MOVQ 24(SP),R10
    0008 (C:\Users\hb19623\Gocode\src\hello\slice.go:6) MOVQ 32(SP),R9
    0009 (C:\Users\hb19623\Gocode\src\hello\slice.go:6) MOVQ 40(SP),BX
    0010 (C:\Users\hb19623\Gocode\src\hello\slice.go:7) MOVQ sfrom+0(SB),AX
    0011 (C:\Users\hb19623\Gocode\src\hello\slice.go:7) MOVQ sfrom+8(SB),DI
    0012 (C:\Users\hb19623\Gocode\src\hello\slice.go:7) MOVQ sfrom+16(SB),BX
    0013 (C:\Users\hb19623\Gocode\src\hello\slice.go:7) MOVQ $0,CX
    0014 (C:\Users\hb19623\Gocode\src\hello\slice.go:7) JMP ,16
    0015 (C:\Users\hb19623\Gocode\src\hello\slice.go:7) INCQ ,CX
    0016 (C:\Users\hb19623\Gocode\src\hello\slice.go:7) CMPQ CX,DI
    0017 (C:\Users\hb19623\Gocode\src\hello\slice.go:7) JGE $0,29
    0018 (C:\Users\hb19623\Gocode\src\hello\slice.go:7) MOVQ (AX),DX
    0019 (C:\Users\hb19623\Gocode\src\hello\slice.go:7) ADDQ $8,AX
    0020 (C:\Users\hb19623\Gocode\src\hello\slice.go:8) MOVQ R10,BX
    0021 (C:\Users\hb19623\Gocode\src\hello\slice.go:8) MOVQ CX,BP
    0022 (C:\Users\hb19623\Gocode\src\hello\slice.go:8) CMPQ CX,R9
    0023 (C:\Users\hb19623\Gocode\src\hello\slice.go:8) JCS $1,26
    0024 (C:\Users\hb19623\Gocode\src\hello\slice.go:8) CALL
      ,runtime.panicindex+0(SB)
    0025 (C:\Users\hb19623\Gocode\src\hello\slice.go:8) UNDEF ,
    0026 (C:\Users\hb19623\Gocode\src\hello\slice.go:8) LEAQ (BX)(BP*8),BX
    0027 (C:\Users\hb19623\Gocode\src\hello\slice.go:8) MOVQ DX,(BX)
    0028 (C:\Users\hb19623\Gocode\src\hello\slice.go:7) JMP ,15
    0029 (C:\Users\hb19623\Gocode\src\hello\slice.go:11) RET ,

    --- prog list "copy2" ---
    0030 (C:\Users\hb19623\Gocode\src\hello\slice.go:13) TEXT
      copy2+0(SB),$120-0
    0031 (C:\Users\hb19623\Gocode\src\hello\slice.go:13) LOCALS ,$56
    0032 (C:\Users\hb19623\Gocode\src\hello\slice.go:13) TYPE
      v+-40(SP){int},$8
    0033 (C:\Users\hb19623\Gocode\src\hello\slice.go:13) TYPE
      autotmp_0007+-56(SP){int},$8
    0034 (C:\Users\hb19623\Gocode\src\hello\slice.go:13) TYPE
      autotmp_0008+-48(SP){int},$8
    0035 (C:\Users\hb19623\Gocode\src\hello\slice.go:13) TYPE
      autotmp_0009+-32(SP){*int},$8
    0036 (C:\Users\hb19623\Gocode\src\hello\slice.go:13) TYPE
      autotmp_0011+-24(SP){[]int},$24
    0037 (C:\Users\hb19623\Gocode\src\hello\slice.go:14) MOVQ
      $type.[]int+0(SB),(SP)
    0038 (C:\Users\hb19623\Gocode\src\hello\slice.go:14) MOVQ $0,8(SP)
    0039 (C:\Users\hb19623\Gocode\src\hello\slice.go:14) MOVQ sfrom+8(SB),BX
    0040 (C:\Users\hb19623\Gocode\src\hello\slice.go:14) MOVQ BX,16(SP)
    0041 (C:\Users\hb19623\Gocode\src\hello\slice.go:14) CALL
      ,runtime.makeslice+0(SB)
    0042 (C:\Users\hb19623\Gocode\src\hello\slice.go:14) MOVQ 24(SP),DX
    0043 (C:\Users\hb19623\Gocode\src\hello\slice.go:14) MOVQ 32(SP),CX
    0044 (C:\Users\hb19623\Gocode\src\hello\slice.go:14) MOVQ 40(SP),AX
    0045 (C:\Users\hb19623\Gocode\src\hello\slice.go:15) MOVQ sfrom+0(SB),DI
    0046 (C:\Users\hb19623\Gocode\src\hello\slice.go:15) MOVQ sfrom+8(SB),BP
    0047 (C:\Users\hb19623\Gocode\src\hello\slice.go:15) MOVQ sfrom+16(SB),BX
    0048 (C:\Users\hb19623\Gocode\src\hello\slice.go:15) MOVQ $0,R8
    0049 (C:\Users\hb19623\Gocode\src\hello\slice.go:15) MOVQ
      BP,autotmp_0008+-48(SP)
    0050 (C:\Users\hb19623\Gocode\src\hello\slice.go:15) JMP ,52
    0051 (C:\Users\hb19623\Gocode\src\hello\slice.go:15) INCQ ,R8
    0052 (C:\Users\hb19623\Gocode\src\hello\slice.go:15) MOVQ
      autotmp_0008+-48(SP),BP
    0053 (C:\Users\hb19623\Gocode\src\hello\slice.go:15) CMPQ R8,BP
    0054 (C:\Users\hb19623\Gocode\src\hello\slice.go:15) JGE $0,98
    0055 (C:\Users\hb19623\Gocode\src\hello\slice.go:15) MOVQ (DI),BP
    0056 (C:\Users\hb19623\Gocode\src\hello\slice.go:15) MOVQ
      R8,autotmp_0007+-56(SP)
    0057 (C:\Users\hb19623\Gocode\src\hello\slice.go:15) MOVQ BP,v+-40(SP)
    0058 (C:\Users\hb19623\Gocode\src\hello\slice.go:15) ADDQ $8,DI
    0059 (C:\Users\hb19623\Gocode\src\hello\slice.go:15) MOVQ
      DI,autotmp_0009+-32(SP)
    0060 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ
      DX,autotmp_0011+-24(SP)
    0061 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ AX,BX
    0062 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) SUBQ CX,BX
    0063 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) CMPQ BX,$1
    0064 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) JGE ,78
    0065 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ
      $type.[]int+0(SB),(SP)
    0066 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ
      autotmp_0011+-24(SP),BX
    0067 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ BX,8(SP)
    0068 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ CX,16(SP)
    0069 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ AX,24(SP)
    0070 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ $1,32(SP)
    0071 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) CALL
      ,runtime.growslice+0(SB)
    0072 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ
      autotmp_0007+-56(SP),R8
    0073 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ
      autotmp_0009+-32(SP),DI
    0074 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ 40(SP),BX
    0075 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ
      BX,autotmp_0011+-24(SP)
    0076 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ 48(SP),CX
    0077 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ 56(SP),AX
    0078 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ CX,DX
    0079 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ CX,BP
    0080 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) INCQ ,BP
    0081 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ AX,CX
    0082 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ BP,AX
    0083 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) CMPQ CX,BP
    0084 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) JCC ,87
    0085 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) CALL
      ,runtime.panicslice+0(SB)
    0086 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) UNDEF ,
    0087 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ
      autotmp_0011+-24(SP),BX
    0088 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ AX,SI
    0089 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ CX,AX
    0090 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ
      BX,autotmp_0011+-24(SP)
    0091 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ
      autotmp_0011+-24(SP),BX
    0092 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) LEAQ (BX)(DX*8),BX
    0093 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ v+-40(SP),BP
    0094 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ BP,(BX)
    0095 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ
      autotmp_0011+-24(SP),DX
    0096 (C:\Users\hb19623\Gocode\src\hello\slice.go:16) MOVQ SI,CX
    0097 (C:\Users\hb19623\Gocode\src\hello\slice.go:15) JMP ,51
    0098 (C:\Users\hb19623\Gocode\src\hello\slice.go:18) RET ,

    --- prog list "copy3" ---
    0099 (C:\Users\hb19623\Gocode\src\hello\slice.go:20) TEXT
      copy3+0(SB),$64-0
    0100 (C:\Users\hb19623\Gocode\src\hello\slice.go:20) LOCALS ,$0
    0101 (C:\Users\hb19623\Gocode\src\hello\slice.go:21) MOVQ sfrom+8(SB),AX
    0102 (C:\Users\hb19623\Gocode\src\hello\slice.go:21) MOVQ
      $type.[]int+0(SB),(SP)
    0103 (C:\Users\hb19623\Gocode\src\hello\slice.go:21) MOVQ AX,8(SP)
    0104 (C:\Users\hb19623\Gocode\src\hello\slice.go:21) MOVQ AX,16(SP)
    0105 (C:\Users\hb19623\Gocode\src\hello\slice.go:21) CALL
      ,runtime.makeslice+0(SB)
    0106 (C:\Users\hb19623\Gocode\src\hello\slice.go:21) MOVQ 24(SP),DX
    0107 (C:\Users\hb19623\Gocode\src\hello\slice.go:21) MOVQ 32(SP),CX
    0108 (C:\Users\hb19623\Gocode\src\hello\slice.go:21) MOVQ 40(SP),AX
    0109 (C:\Users\hb19623\Gocode\src\hello\slice.go:22) MOVQ DX,(SP)
    0110 (C:\Users\hb19623\Gocode\src\hello\slice.go:22) MOVQ CX,8(SP)
    0111 (C:\Users\hb19623\Gocode\src\hello\slice.go:22) MOVQ AX,16(SP)
    0112 (C:\Users\hb19623\Gocode\src\hello\slice.go:22) MOVQ sfrom+0(SB),BX
    0113 (C:\Users\hb19623\Gocode\src\hello\slice.go:22) MOVQ BX,24(SP)
    0114 (C:\Users\hb19623\Gocode\src\hello\slice.go:22) MOVQ sfrom+8(SB),BX
    0115 (C:\Users\hb19623\Gocode\src\hello\slice.go:22) MOVQ BX,32(SP)
    0116 (C:\Users\hb19623\Gocode\src\hello\slice.go:22) MOVQ sfrom+16(SB),BX
    0117 (C:\Users\hb19623\Gocode\src\hello\slice.go:22) MOVQ BX,40(SP)
    0118 (C:\Users\hb19623\Gocode\src\hello\slice.go:22) MOVQ $8,48(SP)
    0119 (C:\Users\hb19623\Gocode\src\hello\slice.go:22) CALL
      ,runtime.copy+0(SB)
    0120 (C:\Users\hb19623\Gocode\src\hello\slice.go:23) RET ,

    --- prog list "copy4" ---
    0121 (C:\Users\hb19623\Gocode\src\hello\slice.go:25) TEXT
      copy4+0(SB),$104-0
    0122 (C:\Users\hb19623\Gocode\src\hello\slice.go:25) LOCALS ,$24
    0123 (C:\Users\hb19623\Gocode\src\hello\slice.go:25) TYPE
      sto+-24(SP){[]int},$24
    0124 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) MOVQ
      $type.[]int+0(SB),(SP)
    0125 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) LEAQ 8(SP),DI
    0126 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) MOVQ $0,AX
    0127 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) STOSQ ,
    0128 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) STOSQ ,
    0129 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) STOSQ ,
    0130 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) MOVQ sfrom+0(SB),BX
    0131 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) MOVQ BX,32(SP)
    0132 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) MOVQ sfrom+8(SB),BX
    0133 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) MOVQ BX,40(SP)
    0134 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) MOVQ sfrom+16(SB),BX
    0135 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) MOVQ BX,48(SP)
    0136 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) CALL
      ,runtime.appendslice+0(SB)
    0137 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) MOVQ 56(SP),BX
    0138 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) MOVQ BX,sto+-24(SP)
    0139 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) MOVQ 64(SP),BX
    0140 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) MOVQ BX,sto+-16(SP)
    0141 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) MOVQ 72(SP),BX
    0142 (C:\Users\hb19623\Gocode\src\hello\slice.go:26) MOVQ BX,sto+-8(SP)
    0143 (C:\Users\hb19623\Gocode\src\hello\slice.go:28) RET ,

    --- prog list "main" ---
    0144 (C:\Users\hb19623\Gocode\src\hello\slice.go:29) TEXT main+0(SB),$0-0
    0145 (C:\Users\hb19623\Gocode\src\hello\slice.go:29) LOCALS ,$0
    0146 (C:\Users\hb19623\Gocode\src\hello\slice.go:31) RET ,

    --- prog list "init" ---
    0147 (C:\Users\hb19623\Gocode\src\hello\slice.go:31) TEXT init+0(SB),$0-0
    0148 (C:\Users\hb19623\Gocode\src\hello\slice.go:31) MOVBQZX
    initdone·+0(SB),AX
    0149 (C:\Users\hb19623\Gocode\src\hello\slice.go:31) LOCALS ,$0
    0150 (C:\Users\hb19623\Gocode\src\hello\slice.go:31) CMPB AX,$0
    0151 (C:\Users\hb19623\Gocode\src\hello\slice.go:31) JEQ ,157
    0152 (C:\Users\hb19623\Gocode\src\hello\slice.go:31) CMPB AX,$2
    0153 (C:\Users\hb19623\Gocode\src\hello\slice.go:31) JNE ,155
    0154 (C:\Users\hb19623\Gocode\src\hello\slice.go:31) RET ,
    0155 (C:\Users\hb19623\Gocode\src\hello\slice.go:31) CALL
      ,runtime.throwinit+0(SB)
    0156 (C:\Users\hb19623\Gocode\src\hello\slice.go:31) UNDEF ,
    0157 (C:\Users\hb19623\Gocode\src\hello\slice.go:31) MOVB
      $2,initdone·+0(SB)
    0158 (C:\Users\hb19623\Gocode\src\hello\slice.go:31) RET ,



    On Fri, May 24, 2013 at 2:38 PM, Ethan Burns wrote:

    Not sure if one is more efficient than the other. If you are just copying
    a slice then I would recommend using the copy variant, because it has the
    best combination of clarity and brevity.


    Best,
    Ethan
    On Friday, May 24, 2013 9:33:53 AM UTC-4, Jan Mercl wrote:

    On Fri, May 24, 2013 at 3:28 PM, Jackie Li wrote:

    dst := append([]T(nil), src...)

    or

    dst := make([]T, len(src))
    copy(dst, src)

    -j

    --
    Jackie

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jan Mercl at May 24, 2013 at 2:11 pm

    On Fri, May 24, 2013 at 4:02 PM, Jackie Li wrote:
    my src is from calculated result. it should be:

    sfrom := []int{1, 2, 3}

    sto1 := make([]int, len(sfrom))
    for i, v := range sfrom {
    sto1[i] = rand.Intn(v)
    }

    sto2 := make([]int, 0, len(sfrom))
    for _, v := range sfrom {
    sto2 = append(sto2, rand.Intn(v)
    }
    It should not make a huge difference, but I believe the first (non
    append) version is better.

    -j

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • André Moraes at May 24, 2013 at 4:34 pm

    On Fri, May 24, 2013 at 11:10 AM, Jan Mercl wrote:
    On Fri, May 24, 2013 at 4:02 PM, Jackie Li wrote:
    my src is from calculated result. it should be:

    sfrom := []int{1, 2, 3}

    sto1 := make([]int, len(sfrom))
    for i, v := range sfrom {
    sto1[i] = rand.Intn(v)
    }

    sto2 := make([]int, 0, len(sfrom))
    for _, v := range sfrom {
    sto2 = append(sto2, rand.Intn(v)
    }
    It should not make a huge difference, but I believe the first (non
    append) version is better.
    Bigger arrays means bigger differences (as expected)

    http://play.golang.org/p/AdI00k1JBt

    len of sfrom: 10
    Index: 2000000 878 ns/op
    Append: 2000000 956 ns/op

    len of sfrom: 100
    Index: 200000 8772 ns/op
    Append: 200000 9397 ns/op

    len of sfrom: 1000
    Index: 20000 84242 ns/op
    Append: 20000 89762 ns/op

    len of sfrom: 1000000
    Index: 20 81233650 ns/op
    Append: 20 83777350 ns/op




    --
    André Moraes
    http://amoraes.info

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dave Cheney at May 24, 2013 at 11:38 pm
    Are you sure you're not benchmarking rand.Intn ?


    On 25/05/2013, at 2:34, André Moraes wrote:
    On Fri, May 24, 2013 at 11:10 AM, Jan Mercl wrote:
    On Fri, May 24, 2013 at 4:02 PM, Jackie Li wrote:
    my src is from calculated result. it should be:

    sfrom := []int{1, 2, 3}

    sto1 := make([]int, len(sfrom))
    for i, v := range sfrom {
    sto1[i] = rand.Intn(v)
    }

    sto2 := make([]int, 0, len(sfrom))
    for _, v := range sfrom {
    sto2 = append(sto2, rand.Intn(v)
    }
    It should not make a huge difference, but I believe the first (non
    append) version is better.
    Bigger arrays means bigger differences (as expected)

    http://play.golang.org/p/AdI00k1JBt

    len of sfrom: 10
    Index: 2000000 878 ns/op
    Append: 2000000 956 ns/op

    len of sfrom: 100
    Index: 200000 8772 ns/op
    Append: 200000 9397 ns/op

    len of sfrom: 1000
    Index: 20000 84242 ns/op
    Append: 20000 89762 ns/op

    len of sfrom: 1000000
    Index: 20 81233650 ns/op
    Append: 20 83777350 ns/op




    --
    André Moraes
    http://amoraes.info

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 24, '13 at 1:29p
activeMay 24, '13 at 11:38p
posts7
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase