问题描述
在Go中,使用类型断言/类型开关的运行速度有多慢,作为运行时类型发现的一种方法?我听说在C / C ++ for例如,在运行时发现类型的性能很差。为了避免这种情况,您通常会将类型成员添加到类中,以便您可以与其进行比较而不是进行转换。
在整个www中我没有找到明确的答案。 。
下面是我询问的一个例子 - 与其他类型的检查方法相比,这是否被认为是快速的(如上所述,或者其他我不知道的)?
func问题(任何接口{}){
switch v: (v)
案例字符串:
案例字符串:
case int32,int64:
fmt.Println(v)
案例SomeCustomType:
fmt.Println(v)
默认值:
fmt.Println(未知)
}
}
编写Benchmark测试来检查它非常容易:
包裹主要
进口(
测试
)
类型myint int64
类型Inccer接口{
inc()
}
func(i * myint)inc(){
* i = * i + 1
}
func BenchmarkIntmethod(b * testing.B){
i:= new(myint)
incnIntmethod(i,bN)
func BenchmarkInterface(b * testing.B){
i:= new(myint)
incnInterface(i,bN)
}
func BenchmarkTypeSwitch(b * testing.B){
i:= new(myint)
incnSwitch(i,bN)
}
func BenchmarkTypeAssertion b * b.b){
i:= new(myint)
incnAssertion(i,bN)
}
func incnIntmethod(i * myint,n int) {
for k:= 0; k< N; k ++ {
i.inc()
}
}
func incnInterface(任何Inccer,n int){
for k:= 0; k< N; k ++ {
any.inc()
}
}
func incnSwitch(任何Inccer,n int){
for k:= 0; k< N; (
case * myint:
v.inc()
}
}
} $ b {
switch v:
$ b func incnAssertion(任何Inccer,n int){
for k:= 0; k< N; k ++ {
if newint,ok:= any。(* myint); OK {
newint.inc()
}
}
}
在我的amd64机器上,我收到以下时间:
$ go test -bench =。
BenchmarkIntmethod 1000000000 2.71 ns / op
BenchmarkInterface 1000000000 2.98 ns / op
BenchmarkTypeSwitch 100000000 16.7 ns / op
BenchmarkTypeAssertion 100000000 13.8 ns / op
因此,通过类型切换或类型断言访问方法看起来比直接或通过接口调用方法慢约5-6倍。
我不知道C ++是否较慢,或者这种放缓对于您的应用程序是否可以容忍。
How slow is using type assertions / type switches in Go, as a method of run-time type discovery?
I've heard that in C/C++ for example, discovering types at run time has bad performance. To bypass that, you usually add type members to classes, so you can compare against these instead of casting.
I haven't found a clear answer for this throughout the www.
Here's an example of what I'm asking about - Is this considered fast when compared to other type checking methodologies (like mentioned above, or others I'm not aware of)?
func question(anything interface{}) {
switch v := anything.(type) {
case string:
fmt.Println(v)
case int32, int64:
fmt.Println(v)
case SomeCustomType:
fmt.Println(v)
default:
fmt.Println("unknown")
}
}
It is very easy to write a Benchmark test to check it: http://play.golang.org/p/E9H_4K2J9-
package main
import (
"testing"
)
type myint int64
type Inccer interface {
inc()
}
func (i *myint) inc() {
*i = *i + 1
}
func BenchmarkIntmethod(b *testing.B) {
i := new(myint)
incnIntmethod(i, b.N)
}
func BenchmarkInterface(b *testing.B) {
i := new(myint)
incnInterface(i, b.N)
}
func BenchmarkTypeSwitch(b *testing.B) {
i := new(myint)
incnSwitch(i, b.N)
}
func BenchmarkTypeAssertion(b *testing.B) {
i := new(myint)
incnAssertion(i, b.N)
}
func incnIntmethod(i *myint, n int) {
for k := 0; k < n; k++ {
i.inc()
}
}
func incnInterface(any Inccer, n int) {
for k := 0; k < n; k++ {
any.inc()
}
}
func incnSwitch(any Inccer, n int) {
for k := 0; k < n; k++ {
switch v := any.(type) {
case *myint:
v.inc()
}
}
}
func incnAssertion(any Inccer, n int) {
for k := 0; k < n; k++ {
if newint, ok := any.(*myint); ok {
newint.inc()
}
}
}
On my amd64 machine, I'm getting the following timing:
$ go test -bench=.
BenchmarkIntmethod 1000000000 2.71 ns/op
BenchmarkInterface 1000000000 2.98 ns/op
BenchmarkTypeSwitch 100000000 16.7 ns/op
BenchmarkTypeAssertion 100000000 13.8 ns/op
So it looks like accessing the method via type switch or type assertion is about 5-6 times slower than calling the method directly or via interface.
I don't know if C++ is slower or if this slowdown is tolerable for your application.
这篇关于Go类型断言/类型开关是否性能不佳/缓慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!