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

     1  package main
     2  
     3  import "testing"
     4  
     5  func newBigStruct() bigStruct {
     6  	var b bigStruct
     7  	for i := 0; i < bigStructSize; i++ {
     8  		b.a[i] = i
     9  	}
    10  	return b
    11  }
    12  
    13  func newBigStructPtr() *bigStruct {
    14  	var b bigStruct
    15  	for i := 0; i < bigStructSize; i++ {
    16  		b.a[i] = i
    17  	}
    18  	return &b
    19  }
    20  
    21  func BenchmarkStructReturnValue(b *testing.B) {
    22  	b.ReportAllocs()
    23  
    24  	t := 0
    25  	for i := 0; i < b.N; i++ {
    26  		v := newBigStruct()
    27  		t += v.a[0]
    28  	}
    29  }
    30  
    31  func BenchmarkStructReturnPointer(b *testing.B) {
    32  	b.ReportAllocs()
    33  
    34  	t := 0
    35  	for i := 0; i < b.N; i++ {
    36  		v := newBigStructPtr()
    37  		t += v.a[0]
    38  	}
    39  }