github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/prepare/11_reflect/test/reflect_test.go (about)

     1  package test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  // go test -bench=. -benchmem
     9  
    10  type Student struct {
    11  	Name  string
    12  	Age   int
    13  	Class string
    14  	Score int
    15  }
    16  
    17  func BenchmarkReflect_New(b *testing.B) {
    18  	var s *Student
    19  	sv := reflect.TypeOf(Student{})
    20  	b.ResetTimer()
    21  	for i := 0; i < b.N; i++ {
    22  		sn := reflect.New(sv)
    23  		s, _ = sn.Interface().(*Student)
    24  	}
    25  	_ = s
    26  }
    27  
    28  func BenchmarkDirect_New(b *testing.B) {
    29  	var s *Student
    30  	b.ResetTimer()
    31  	for i := 0; i < b.N; i++ {
    32  		s = new(Student)
    33  	}
    34  	_ = s
    35  }
    36  
    37  func BenchmarkReflect_Set(b *testing.B) {
    38  	var s *Student
    39  	sv := reflect.TypeOf(Student{})
    40  	b.ResetTimer()
    41  	for i := 0; i < b.N; i++ {
    42  		sn := reflect.New(sv)
    43  		s = sn.Interface().(*Student)
    44  		s.Name = "Jerry"
    45  		s.Age = 18
    46  		s.Class = "20005"
    47  		s.Score = 100
    48  	}
    49  }
    50  
    51  func BenchmarkReflect_SetFieldByName(b *testing.B) {
    52  	sv := reflect.TypeOf(Student{})
    53  	b.ResetTimer()
    54  	for i := 0; i < b.N; i++ {
    55  		sn := reflect.New(sv).Elem()
    56  		sn.FieldByName("Name").SetString("Jerry")
    57  		sn.FieldByName("Age").SetInt(18)
    58  		sn.FieldByName("Class").SetString("20005")
    59  		sn.FieldByName("Score").SetInt(100)
    60  	}
    61  }
    62  
    63  func BenchmarkReflect_SetFieldByIndex(b *testing.B) {
    64  	sv := reflect.TypeOf(Student{})
    65  	b.ResetTimer()
    66  	for i := 0; i < b.N; i++ {
    67  		sn := reflect.New(sv).Elem()
    68  		sn.Field(0).SetString("Jerry")
    69  		sn.Field(1).SetInt(18)
    70  		sn.Field(2).SetString("20005")
    71  		sn.Field(3).SetInt(100)
    72  	}
    73  }
    74  func BenchmarkDirect_Set(b *testing.B) {
    75  	var s *Student
    76  	b.ResetTimer()
    77  	for i := 0; i < b.N; i++ {
    78  		s = new(Student)
    79  		s.Name = "Jerry"
    80  		s.Age = 18
    81  		s.Class = "20005"
    82  		s.Score = 100
    83  	}
    84  }
    85  
    86  // 直接调用
    87  func DirectInvoke(s *Student) {
    88  	s.Name = "Jerry"
    89  	s.Age = 18
    90  	s.Class = "20005"
    91  	s.Score = 100
    92  }
    93  
    94  // 拆箱调用
    95  func InterfaceInvoke(i interface{}) {
    96  	s := i.(*Student)
    97  	s.Name = "Jerry"
    98  	s.Age = 18
    99  	s.Class = "20005"
   100  	s.Score = 100
   101  }
   102  
   103  func BenchmarkDirectInvoke(b *testing.B) {
   104  	s := new(Student)
   105  	for i := 0; i < b.N; i++ {
   106  		DirectInvoke(s)
   107  	}
   108  	_ = s
   109  }
   110  func BenchmarkInterfaceInvoke(b *testing.B) {
   111  	s := new(Student)
   112  	for i := 0; i < b.N; i++ {
   113  		InterfaceInvoke(s)
   114  	}
   115  	_ = s
   116  }