성능 문제
[런타임 유형 발견은 성과 처벌을받을 수 있습니다. GO 프로그래머는 유형의 어설 션 또는 유형 스위치가 비슷한 비 효율성을 보이는지 여부를 숙고했습니다. 이를 해결하기 위해 포괄적 인 벤치 마크를 수행하겠습니다 :
패키지 메인 수입 (수입) "FMT" "테스트" )) func question (alling interface {}) { 스위치 V : = ally. (유형) { 사례 문자열 : fmt.println (V) 사례 int32, int64 : fmt.println (V) 사례 somecustomType : fmt.println (V) 기본: fmt.println ( "알 수없는") } }
package main import ( "fmt" "testing" ) 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") } }샘플 코드는 유형 스위치를 사용하여 입력 변수 유형을 결정합니다. 수입 (수입) "테스트" )) myint int64를 입력하십시오 유형 Inccer 인터페이스 { 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) } funcnintmethod (i *myint, n int) { K : = 0; k
BenchmarkintMethod-16 20000000000 1.67 NS/OP BenchmarkInterface-16 10000000000 2.03 NS/OP BenchmarkTypeswitch-16 20000000000 1.70 NS/OP BenchmarkTypeassertion-16 20000000000 1.67 NS/OP
따라서, 우리의 결론은 다른 유형 검사 방법론과 비교할 때 유형의 어설 션 및 유형 스위치가 눈에 띄는 성능 손상을 일으키지 않는다는 것입니다.부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.
Copyright© 2022 湘ICP备2022001581号-3