github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/bench/iface/iface_test.go (about) 1 package iface 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 func BenchmarkStruct_Direct(b *testing.B) { 9 var s = &Struct{10} 10 for n := 0; n < b.N; n++ { 11 s.x = s.x 12 s.x = s.x 13 s.x = s.x 14 s.x = s.x 15 s.x = s.x 16 s.x = s.x 17 s.x = s.x 18 s.x = s.x 19 s.x = s.x 20 s.x = s.x 21 } 22 } 23 24 func BenchmarkStruct_SetDirect(b *testing.B) { 25 var s = &Struct{10} 26 for n := 0; n < b.N; n++ { 27 s.x = 0 28 s.x = 1 29 s.x = 2 30 s.x = 3 31 s.x = 4 32 s.x = 5 33 s.x = 6 34 s.x = 7 35 s.x = 8 36 s.x = 9 37 } 38 } 39 40 func BenchmarkStruct_Get(b *testing.B) { 41 var s = &Struct{10} 42 for n := 0; n < b.N; n++ { 43 s.Get() 44 s.Get() 45 s.Get() 46 s.Get() 47 s.Get() 48 s.Get() 49 s.Get() 50 s.Get() 51 s.Get() 52 s.Get() 53 } 54 } 55 56 func BenchmarkStruct_Set(b *testing.B) { 57 var s = &Struct{10} 58 for n := 0; n < b.N; n++ { 59 s.Set(n + 0) 60 s.Set(n + 1) 61 s.Set(n + 2) 62 s.Set(n + 3) 63 s.Set(n + 4) 64 s.Set(n + 5) 65 s.Set(n + 6) 66 s.Set(n + 7) 67 s.Set(n + 8) 68 s.Set(n + 9) 69 } 70 } 71 72 func BenchmarkStruct_GetCopy(b *testing.B) { 73 var s = Struct{10} 74 for n := 0; n < b.N; n++ { 75 s.CopyGet() 76 s.CopyGet() 77 s.CopyGet() 78 s.CopyGet() 79 s.CopyGet() 80 s.CopyGet() 81 s.CopyGet() 82 s.CopyGet() 83 s.CopyGet() 84 s.CopyGet() 85 } 86 } 87 88 func BenchmarkInterface_Get(b *testing.B) { 89 s := NewStruct(b.N & 1) 90 for n := 0; n < b.N; n++ { 91 s.Get() 92 s.Get() 93 s.Get() 94 s.Get() 95 s.Get() 96 s.Get() 97 s.Get() 98 s.Get() 99 s.Get() 100 s.Get() 101 } 102 } 103 104 func BenchmarkInterface_CopyGet(b *testing.B) { 105 s := NewStruct(b.N & 1) 106 for n := 0; n < b.N; n++ { 107 s.CopyGet() 108 s.CopyGet() 109 s.CopyGet() 110 s.CopyGet() 111 s.CopyGet() 112 s.CopyGet() 113 s.CopyGet() 114 s.CopyGet() 115 s.CopyGet() 116 s.CopyGet() 117 } 118 } 119 120 func BenchmarkInterface_Set(b *testing.B) { 121 s := NewStruct(b.N & 1) 122 for n := 0; n < b.N; n++ { 123 s.Set(0) 124 s.Set(0) 125 s.Set(0) 126 s.Set(0) 127 s.Set(0) 128 s.Set(0) 129 s.Set(0) 130 s.Set(0) 131 s.Set(0) 132 s.Set(0) 133 } 134 } 135 136 func BenchmarkSetXReflection(b *testing.B) { 137 var s interface{} = &Struct{10} 138 //var t *Struct 139 for n := 0; n < b.N; n++ { 140 switch v := s.(type) { 141 case string: 142 fmt.Println(v) 143 case int32, int64: 144 fmt.Println(v) 145 case *Struct: 146 s.(*Struct).Set(0) 147 //t.Set(0) 148 default: 149 fmt.Printf("unknown %T\n", v) 150 } 151 switch v := s.(type) { 152 case string: 153 fmt.Println(v) 154 case int32, int64: 155 fmt.Println(v) 156 case *Struct: 157 s.(*Struct).Set(0) 158 //t.Set(0) 159 default: 160 fmt.Printf("unknown %T\n", v) 161 } 162 switch v := s.(type) { 163 case string: 164 fmt.Println(v) 165 case int32, int64: 166 fmt.Println(v) 167 case *Struct: 168 s.(*Struct).Set(0) 169 //t.Set(0) 170 default: 171 fmt.Printf("unknown %T\n", v) 172 } 173 switch v := s.(type) { 174 case string: 175 fmt.Println(v) 176 case int32, int64: 177 fmt.Println(v) 178 case *Struct: 179 s.(*Struct).Set(0) 180 //t.Set(0) 181 default: 182 fmt.Printf("unknown %T\n", v) 183 } 184 switch v := s.(type) { 185 case string: 186 fmt.Println(v) 187 case int32, int64: 188 fmt.Println(v) 189 case *Struct: 190 s.(*Struct).Set(0) 191 //t.Set(0) 192 default: 193 fmt.Printf("unknown %T\n", v) 194 } 195 switch v := s.(type) { 196 case string: 197 fmt.Println(v) 198 case int32, int64: 199 fmt.Println(v) 200 case *Struct: 201 s.(*Struct).Set(0) 202 //t.Set(0) 203 default: 204 fmt.Printf("unknown %T\n", v) 205 } 206 switch v := s.(type) { 207 case string: 208 fmt.Println(v) 209 case int32, int64: 210 fmt.Println(v) 211 case *Struct: 212 s.(*Struct).Set(0) 213 //t.Set(0) 214 default: 215 fmt.Printf("unknown %T\n", v) 216 } 217 switch v := s.(type) { 218 case string: 219 fmt.Println(v) 220 case int32, int64: 221 fmt.Println(v) 222 case *Struct: 223 s.(*Struct).Set(0) 224 //t.Set(0) 225 default: 226 fmt.Printf("unknown %T\n", v) 227 } 228 switch v := s.(type) { 229 case string: 230 fmt.Println(v) 231 case int32, int64: 232 fmt.Println(v) 233 case *Struct: 234 s.(*Struct).Set(0) 235 //t.Set(0) 236 default: 237 fmt.Printf("unknown %T\n", v) 238 } 239 switch v := s.(type) { 240 case string: 241 fmt.Println(v) 242 case int32, int64: 243 fmt.Println(v) 244 case *Struct: 245 s.(*Struct).Set(0) 246 //t.Set(0) 247 default: 248 fmt.Printf("unknown %T\n", v) 249 } 250 251 } 252 }