github.com/goccy/go-reflect@v1.2.1-0.20220925055700-4646ad15ec8a/benchmark_test.go (about)

     1  package reflect_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	goreflect "github.com/goccy/go-reflect"
     8  )
     9  
    10  func kindFromReflect(v interface{}) reflect.Kind {
    11  	return reflect.TypeOf(v).Kind()
    12  }
    13  
    14  func kindFromGoReflect(v interface{}) goreflect.Kind {
    15  	return goreflect.TypeOf(v).Kind()
    16  }
    17  
    18  func f(_ interface{}) {}
    19  
    20  func valueFromReflect(v interface{}) {
    21  	f(reflect.ValueOf(v).Elem())
    22  }
    23  
    24  func valueFromGoReflect(v interface{}) {
    25  	f(goreflect.ValueNoEscapeOf(v).Elem())
    26  }
    27  
    28  func Benchmark_TypeOf_Reflect(b *testing.B) {
    29  	b.ReportAllocs()
    30  	for n := 0; n < b.N; n++ {
    31  		var v struct {
    32  			i int
    33  		}
    34  		kindFromReflect(&v)
    35  	}
    36  }
    37  
    38  func Benchmark_TypeOf_GoReflect(b *testing.B) {
    39  	b.ReportAllocs()
    40  	for n := 0; n < b.N; n++ {
    41  		var v struct {
    42  			i int
    43  		}
    44  		kindFromGoReflect(&v)
    45  	}
    46  }
    47  
    48  func Benchmark_ValueOf_Reflect(b *testing.B) {
    49  	b.ReportAllocs()
    50  	for n := 0; n < b.N; n++ {
    51  		valueFromReflect(&struct {
    52  			I int
    53  			F float64
    54  		}{I: 10})
    55  	}
    56  }
    57  
    58  func Benchmark_ValueOf_GoReflect(b *testing.B) {
    59  	b.ReportAllocs()
    60  	for n := 0; n < b.N; n++ {
    61  		valueFromGoReflect(&struct {
    62  			I int
    63  			F float64
    64  		}{I: 10})
    65  	}
    66  }