github.com/iainanderson83/datastructures@v0.0.4-0.20191103204413-889e20b53bcf/arrays/slice_test.go (about)

     1  package main
     2  
     3  import "testing"
     4  
     5  func BenchmarkSlicePointers(b *testing.B) {
     6  	b.ReportAllocs()
     7  
     8  	for i := 0; i < b.N; i++ {
     9  		slice := make([]*A, 0, 100)
    10  		for j := 0; j < 100; j++ {
    11  			slice = append(slice, &A{B: j, C: j + 1})
    12  		}
    13  	}
    14  }
    15  
    16  func BenchmarkSliceNoPointers(b *testing.B) {
    17  	b.ReportAllocs()
    18  
    19  	for i := 0; i < b.N; i++ {
    20  		slice := make([]A, 0, 100)
    21  		for j := 0; j < 100; j++ {
    22  			slice = append(slice, A{B: j, C: j + 1})
    23  		}
    24  	}
    25  }
    26  
    27  func BenchmarkSliceHybrid(b *testing.B) {
    28  	b.ReportAllocs()
    29  
    30  	for i := 0; i < b.N; i++ {
    31  		slice := make([]A, 0, 100)
    32  		for j := 0; j < 100; j++ {
    33  			slice = append(slice, A{B: j, C: j + 1})
    34  		}
    35  
    36  		slicep := make([]*A, len(slice))
    37  		for j := range slice {
    38  			slicep[j] = &slice[j]
    39  		}
    40  	}
    41  }