Does a type assertion / type switch have bad performance / is slow in Go?

前端 未结 6 764
天涯浪人
天涯浪人 2020-12-07 22:04

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

6条回答
  •  没有蜡笔的小新
    2020-12-07 22:50

    I run bench example by @siritinga in my laptop (go1.7.3 linux/amd64), got this result:

    $ go test -bench .
    BenchmarkIntmethod-4            2000000000               1.99 ns/op
    BenchmarkInterface-4            1000000000               2.30 ns/op
    BenchmarkTypeSwitch-4           2000000000               1.80 ns/op
    BenchmarkTypeAssertion-4        2000000000               1.67 ns/op
    

提交回复
热议问题