(Go) do not use append to insert elements

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The title seems a little bluffing ....
The cause was that I was doing an insert function for slice, and I did some performance testing for several versions of the package. The results are unexpected!

Import ("FMT"    "Reflect"    "Time") Func Copyinsert (Slice interface{}, pos int, Value interface{}) interface{} {V: = Reflect. ValueOf(slice) v = reflect. Append(V, reflect. ValueOf(value)) Reflect. Copy(V. Slice(pos+1V. Len()), V. Slice(POS, V. Len())) v. Index(POS). Set(Reflect. ValueOf(value)) Return V. Interface()}func Insert (Slice interface{}, pos int, Value interface{}) interface{} {V: = Reflect. ValueOf(slice) NE: = reflect. Makeslice(Reflect. Sliceof(Reflect. TypeOf(value)),1,1) NE. Index(0). Set(Reflect. ValueOf(value)) v = Reflect. Appendslice(V. Slice(0, POS), reflect. Appendslice(NE, V. Slice(POS, V. Len())) return V. Interface()}func main () {slice: = []int{1,2} slice2: = []int{1,2} slice3: = []int{1,2} T0: = Time. now() for I: =1; i < 10000; i++ {Slice = Append (slice[:1], append ([]int{i}, slice[1:]...)    ...) } T1: = time. now() for I: =1; i < 10000; i++ {Slice2 = Insert (Slice2,1, i).    ([]int)} T2: = Time. now() for I: =1; i < 10000; i++ {Slice3 = Copyinsert (Slice3,1, i). ([]int)//FMT. Println(SLICE3)} T3: = time. now()//element detection for I: =0; i < 10000; i++ {If slice[i]! = Slice2[i] | | Slice2[i]! = Slice3[i] {FMT. Println("Error")}} FMT. Println("reflect append insert:", T2. Sub(t1),"Append insert:", T1. Sub(t0),"Copy Insert:", T3. Sub(T2))}

Output results

append insert: 273.276271append 195.938231copy 29.626058ms

The use of reflective reflect than ordinary append is understandable, but actually 10 times times slower than copy Instert. >_<.

I didn't believe it, so I did the test.

Import ("FMT"    "Time") Func Main () {s1: = []int{1,2,3} s2: = []int{1,2,3}//Insert the third T1: = time. Now () forI: =0; I <10000; i++ {S1 = append (s1[0:3], append ([]int{i}, s1[3:]...)...)} t2: = time. Now ()//Every time you insert a third forI: =0; I <10000; i++ {s2 = append (s2, i) copy (s2[3: Len (S2)], s2[2: Len (S2)-1]) s2[3] = i} t3: = time. Now ()//Detection error forI: =0; I < Len (S1); i++ {ifS1[i]! = S2[i] {fmt. Println ("Error")}} FMT. Println (T3. Sub (T2), T2. Sub (t1))}

Output results

copy instert: 21.245765append 189.930674ms

The result is still 10 times times worse!

Guess: two times append (), doing slice slices will produce the underlying array copy. (Append ([]int{i}, S1[3:] ...) ), but when the S1 is very large, the more copying, the worse the performance, and copy is using the original underlying array. >_<.

Welcome to Daniel's advice ....

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.