伪三元表达式 性能分析案例

如何去分析一个func 的三元表达式的效果比 map的方式更加性能高? Q1: benchmark 好像也看不出什么,可能例子太简单的原因?感谢 楼下朋友指出问题哈 Q2 string对象和interface 断言居然也没什么消耗差距。

package main

import "testing"

func ternary(x int) string {
    if x > 0 {
        return "a"
    }
    return "b"
}

func TernaryInterface(statement bool, a, b interface{}) interface{} {
    if statement {
        return a
    }
    return b
}

func BenchmarkTernaryInterface(b *testing.B) {
    var y string
    for i := 0; i < b.N; i++ {
        y = TernaryInterface(1 > 0, "a", "b").(string)
    }
    _ = y
}
func BenchmarkTernaryWithFunc1(b *testing.B) {
    var y string
    for i := 0; i < b.N; i++ {
        y = ternary(1)
    }
    _ = y
}
func BenchmarkTernaryMap1(b *testing.B) {
    var y string
    for i := 0; i < b.N; i++ {
        y = map[bool]string{true: "a", false: "b"}[1 > 0]
    }
    _ = y
}

go test -bench=. -benchmem -cpu 1,2,4,8
goos: darwin
goarch: amd64
BenchmarkTernaryInterface       2000000000         0.32 ns/op            0 B/op          0 allocs/op
BenchmarkTernaryInterface-2     2000000000        0.32 ns/op            0 B/op          0 allocs/op
BenchmarkTernaryInterface-4     2000000000        0.32 ns/op            0 B/op          0 allocs/op
BenchmarkTernaryInterface-8     2000000000        0.32 ns/op            0 B/op          0 allocs/op
BenchmarkTernaryWithFunc1       2000000000       0.32 ns/op            0 B/op          0 allocs/op
BenchmarkTernaryWithFunc1-2     2000000000      0.32 ns/op            0 B/op          0 allocs/op
BenchmarkTernaryWithFunc1-4     2000000000      0.32 ns/op            0 B/op          0 allocs/op
BenchmarkTernaryWithFunc1-8     2000000000      0.32 ns/op            0 B/op          0 allocs/op
BenchmarkTernaryMap1            20000000              103 ns/op               0 B/op          0 allocs/op
BenchmarkTernaryMap1-2          20000000             104 ns/op               0 B/op          0 allocs/op
BenchmarkTernaryMap1-4          20000000             102 ns/op               0 B/op          0 allocs/op
BenchmarkTernaryMap1-8          20000000             102 ns/op               0 B/op          0 allocs/op
PASS
已邀请:

luw2007

赞同来自: haohongfan wdxxl

差别很大吧。从map中查找需要20多ns。

goos: darwin
goarch: amd64
BenchmarkTernaryWithFunc-4      2000000000           0.34 ns/op        0 B/op          0 allocs/op
BenchmarkTernaryMap-4           50000000            29.0 ns/op         0 B/op          0 allocs/op
PASS
ok      command-line-arguments  2.231s

bench 写的有点问题,我改了一下。

$> cat tr_test.go
package main

import "testing"

var (
    M = map[bool]string{true: "a", false: "b"}
)

func ternary(x int) string {
    if x > 0 {
        return "a"
    }
    return "b"
}

func ternaryMap(x int) string {
    return M[x > 0]
}

func BenchmarkTernaryWithFunc(b *testing.B) {
    var y string
    for i := 0; i < b.N; i++ {
        y = ternary(1)
    }
    _ = y
}

func BenchmarkTernaryMap(b *testing.B) {
    var y string
    for i := 0; i < b.N; i++ {
        y = ternaryMap(1)
    }
    _ = y
}

haohongfan - talk is expensive, just show code

赞同来自:

我觉得没啥可比性吧, 用map可以分解成

x := 1
//y := map[bool]string{true: "a", false: "b"}[x > 0]
h := map[bool]string{true: "a", false: "b"}
fmt.Println(h[x > 0])

只是写成了一行

要回复问题请先登录注册