go test -bench=. -benchmem
BenchmarkTypeof-4 10000000 153 ns/op 16 B/op 1 allocs/op
BenchmarkReflectTypeOf-4 50000000 27.4 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/mrap/bench/typeof 3.099s
go test -bench=. -benchmem
BenchmarkTypeof-4 10000000 153 ns/op 16 B/op 1 allocs/op
BenchmarkReflectTypeOf-4 50000000 27.4 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/mrap/bench/typeof 3.099s
package typeof_test | |
import ( | |
"fmt" | |
"reflect" | |
"testing" | |
) | |
type T struct { | |
} | |
func typeof(v interface{}) string { | |
return fmt.Sprintf("%T", v) | |
} | |
func reflectTypeof(v interface{}) string { | |
return reflect.TypeOf(v).String() | |
} | |
func BenchmarkTypeof(b *testing.B) { | |
var v T | |
for i := 0; i < b.N; i++ { | |
typeof(v) | |
} | |
} | |
func BenchmarkReflectTypeOf(b *testing.B) { | |
var v T | |
for i := 0; i < b.N; i++ { | |
reflectTypeof(v) | |
} | |
} |