golang笔记之interface类型判断性能


Table of Contents

测试代码

package main

import "testing"

type InterfaceA interface {
    AA()
}

type InterfaceB interface {
    BB()
}

type A struct {
    v int
}

func (a *A) AA() {
    a.v += 1
}

func BenchmarkTypeSwitch(b *testing.B) {
    var a = new(A)

    switchFunc := func(v interface{}) {
        switch v.(type) {
        case InterfaceA:
            v.(InterfaceA).AA()
        case InterfaceB:
            v.(InterfaceB).BB()
        }
    }
    for i := 0; i < b.N; i++ {
        switchFunc(a)
    }
}

func BenchmarkNormalSwitch(b *testing.B) {
    var a = new(A)

    switchFunc := func(v *A) {
        v.AA()
    }
    for i := 0; i < b.N; i++ {
        switchFunc(a)
    }
}

func BenchmarkInterfaceSwitch(b *testing.B) {
    var a = new(A)

    switchFunc := func(v interface{}) {
        v.(InterfaceA).AA()
    }
    for i := 0; i < b.N; i++ {
        switchFunc(a)
    }
}

func BenchmarkInterfaceSwitch1(b *testing.B) {
    var a = new(A)

    switchFunc := func(v interface{}) {
        v.(*A).AA()
    }
    for i := 0; i < b.N; i++ {
        switchFunc(a)
    }
}

func BenchmarkInterfaceSwitch2(b *testing.B) {
    var a = new(A)

    switchFunc := func(v InterfaceA) {
        v.(*A).AA()
    }
    for i := 0; i < b.N; i++ {
        switchFunc(a)
    }
}

func BenchmarkInterfaceSwitch3(b *testing.B) {
    var a = new(A)

    switchFunc := func(v InterfaceA) {
        v.AA()
    }
    for i := 0; i < b.N; i++ {
        switchFunc(a)
    }
}

func BenchmarkNormalListSwitch(b *testing.B) {
    a := make([]*A, 10000)
    for i := range a {
        a[i] = new(A)
    }

    switchFunc := func(vv []*A) {
        for _, v := range vv {
            v.AA()
        }
    }
    for i := 0; i < b.N; i++ {
        switchFunc(a)
    }
}

func BenchmarkInterfaceListSwitch(b *testing.B) {
    a := make([]*A, 10000)
    for i := range a {
        a[i] = new(A)
    }

    switchFunc := func(vv interface{}) {
        for _, v := range vv.([]*A) {
            v.AA()
        }
    }
    for i := 0; i < b.N; i++ {
        switchFunc(a)
    }
}

func BenchmarkInterfaceListSwitch1(b *testing.B) {
    a := make([]InterfaceA, 10000)
    for i := range a {
        a[i] = new(A)
    }

    switchFunc := func(vv interface{}) {
        for _, v := range vv.([]InterfaceA) {
            v.AA()
        }
    }
    for i := 0; i < b.N; i++ {
        switchFunc(a)
    }
}

func BenchmarkInterfaceListSwitch2(b *testing.B) {
    a := make([]InterfaceA, 10000)
    for i := range a {
        a[i] = new(A)
    }

    switchFunc := func(vv []InterfaceA) {
        for _, v := range vv {
            v.AA()
        }
    }
    for i := 0; i < b.N; i++ {
        switchFunc(a)
    }
}

测试结果

└──╼ go test -test.bench=".*" .
goos: darwin
goarch: amd64
pkg: log2
cpu: Intel(R) Core(TM) i5-8279U CPU @ 2.40GHz
BenchmarkTypeSwitch-8                 72261417            16.89 ns/op
BenchmarkNormalSwitch-8               861871533            1.384 ns/op
BenchmarkInterfaceSwitch-8            148360124            8.370 ns/op
BenchmarkInterfaceSwitch1-8           780127671            1.537 ns/op
BenchmarkInterfaceSwitch2-8           774663793            1.538 ns/op
BenchmarkInterfaceSwitch3-8           739255111            1.628 ns/op
BenchmarkNormalListSwitch-8             225832          5346 ns/op
BenchmarkInterfaceListSwitch-8          252312          4820 ns/op
BenchmarkInterfaceListSwitch1-8          61461         19800 ns/op
BenchmarkInterfaceListSwitch2-8          60193         20142 ns/op
PASS
ok    log2	14.670s
作者: honmaple
链接: https://honmaple.me/articles/2020/08/golang笔记之interface类型判断性能.html
版权: CC BY-NC-SA 4.0 知识共享署名-非商业性使用-相同方式共享4.0国际许可协议
wechat
alipay

加载评论