8-3 使用pprof进行性能调优

一、介绍pprof

go test -bench . -cpuprofile cpu.out

go tool pprof cpu.out

指定某个测试_test.go文件

go test -bench  . -v nonrepeating_end_test.go

 go test -bench . -cpuprofile cpu.out -v nonrepeating_end_test.go

测试更多方面的信息

go test -bench . -cpuprofile cpu.out

查看CPU结果

go tool pprof cpu.out  //此时进入交互式命令,可以输入help查看帮助
web  //自动弹出网页展示svg图查看结果

注意,如果报错:Failed to execute dot. Is Graphviz installed? Error: exec: "dot": executable file not found in %PATH%
请到http://www.graphviz.org/download/下载Graphviz, 并配置到环境变量中,将bin目录配置到PATH。

二、实践

 

1.第一个版本代码

 

package main

import (
    "testing"
)
func lengthOfNonRepeatingSubStr(s string) int {
    //lastOccurred := make(map[byte]int)
    lastOccurred := make(map[rune]int)
    start := 0
    maxLength := 0

    //for i, ch := range []byte(s) {
    for i, ch := range []rune(s) {
        if lastI, ok := lastOccurred[ch]; ok && lastI >= start {
            start = lastOccurred[ch] + 1
        }
        if i-start+1 > maxLength {
            maxLength = i - start + 1
        }
        lastOccurred[ch] = i
    }
    return maxLength
}
func TestSubstr(t *testing.T) {
    tests := []struct {
        s string
        ans int
    } {
        // Normal cases
        {"abcabcbb", 3},
        {"pwwkew", 3},
        // Edge cases
        {"", 0},
        {"b", 1},
        {"bbbbbbbb", 1},
        {"abcabcabcd", 4},
        // Chinense cases
        {"这里是慕课网", 6},
        {"一二三二一", 3},
        {"黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花", 8},
    }

    for _, tt := range tests {
        actual := lengthOfNonRepeatingSubStr(tt.s)
        if actual != tt.ans {
            t.Errorf("got %d for input %s; " +
                "expected %d",
                actual, tt.s, tt.ans)
        }
    }
}

func BenchmarkSubstr(b *testing.B) {
    s := "黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花"
    ans := 8
    for i := 0; i < b.N; i++ {
        actual := lengthOfNonRepeatingSubStr(s)
        if actual != ans {
            b.Errorf("got %d for input %s; " +
                "expected %d",
                actual, s, ans)
        }
    }
}

8-3 使用pprof进行性能调优_第1张图片

8-3 使用pprof进行性能调优_第2张图片

发现性能差在map赋值,rune的utf8 decode

2.

将map(hash实现)修改slice

package main

import (
    "testing"
)

func lengthOfNonRepeatingSubStr(s string) int {
    //lastOccurred := make(map[byte]int)
    //lastOccurred := make(map[rune]int)
    // 测试性能优化
    // stores las occurred pos + 1.
    // 0 means not seen
    var lastOccurred = make([]int, 0xffff)
    start := 0
    maxLength := 0

    //for i, ch := range []byte(s) {
    for i, ch := range []rune(s) {
        if lastI := lastOccurred[ch]; lastI > start {
            start = lastI
        }
        if i-start+1 > maxLength {
            maxLength = i - start + 1
        }
        lastOccurred[ch] = i + 1
    }
    return maxLength
}
func TestSubstr(t *testing.T) {
    tests := []struct {
        s string
        ans int
    } {
        // Normal cases
        {"abcabcbb", 3},
        {"pwwkew", 3},
        // Edge cases
        {"", 0},
        {"b", 1},
        {"bbbbbbbb", 1},
        {"abcabcabcd", 4},
        // Chinense cases
        {"这里是慕课网", 6},
        {"一二三二一", 3},
        {"黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花", 8},
    }

    for _, tt := range tests {
        actual := lengthOfNonRepeatingSubStr(tt.s)
        if actual != tt.ans {
            t.Errorf("got %d for input %s; " +
                "expected %d",
                actual, tt.s, tt.ans)
        }
    }
}

func BenchmarkSubstr(b *testing.B) {
    s := "黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花"
    ans := 8
    for i := 0; i < b.N; i++ {
        actual := lengthOfNonRepeatingSubStr(s)
        if actual != ans {
            b.Errorf("got %d for input %s; " +
                "expected %d",
                actual, s, ans)
        }
    }
}

8-3 使用pprof进行性能调优_第3张图片

8-3 使用pprof进行性能调优_第4张图片

发现图像变得复杂,耗时花在gc上,垃圾回收

3.

package main

import (
    "testing"
)

var lastOccurred = make([]int, 0xffff)
func lengthOfNonRepeatingSubStr(s string) int {
    //lastOccurred := make(map[byte]int)
    //lastOccurred := make(map[rune]int)
    // 测试性能优化
    // stores las occurred pos + 1.
    // 0 means not seen
    for i := range lastOccurred {
        lastOccurred[i] = 0
    }
    start := 0
    maxLength := 0

    //for i, ch := range []byte(s) {
    for i, ch := range []rune(s) {
        if lastI := lastOccurred[ch]; lastI > start {
            start = lastI
        }
        if i-start+1 > maxLength {
            maxLength = i - start + 1
        }
        lastOccurred[ch] = i + 1
    }
    return maxLength
}
func TestSubstr(t *testing.T) {
    tests := []struct {
        s string
        ans int
    } {
        // Normal cases
        {"abcabcbb", 3},
        {"pwwkew", 3},
        // Edge cases
        {"", 0},
        {"b", 1},
        {"bbbbbbbb", 1},
        {"abcabcabcd", 4},
        // Chinense cases
        {"这里是慕课网", 6},
        {"一二三二一", 3},
        {"黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花", 8},
    }

    for _, tt := range tests {
        actual := lengthOfNonRepeatingSubStr(tt.s)
        if actual != tt.ans {
            t.Errorf("got %d for input %s; " +
                "expected %d",
                actual, tt.s, tt.ans)
        }
    }
}

func BenchmarkSubstr(b *testing.B) {
    s := "黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花"
    ans := 8
    for i := 0; i < b.N; i++ {
        actual := lengthOfNonRepeatingSubStr(s)
        if actual != ans {
            b.Errorf("got %d for input %s; " +
                "expected %d",
                actual, s, ans)
        }
    }
}

8-3 使用pprof进行性能调优_第5张图片

8-3 使用pprof进行性能调优_第6张图片

可以发现耗时消耗在

    for i := range lastOccurred {
        lastOccurred[i] = 0
    }

所以我们,准备一个长的字符串,保证最大的耗时不是在memclr

4.

package main

import (
    "testing"
)

var lastOccurred = make([]int, 0xffff)
func lengthOfNonRepeatingSubStr(s string) int {
    //lastOccurred := make(map[byte]int)
    //lastOccurred := make(map[rune]int)
    // 测试性能优化
    // stores las occurred pos + 1.
    // 0 means not seen
    for i := range lastOccurred {
        lastOccurred[i] = 0
    }
    start := 0
    maxLength := 0

    //for i, ch := range []byte(s) {
    for i, ch := range []rune(s) {
        if lastI := lastOccurred[ch]; lastI > start {
            start = lastI
        }
        if i-start+1 > maxLength {
            maxLength = i - start + 1
        }
        lastOccurred[ch] = i + 1
    }
    return maxLength
}
func TestSubstr(t *testing.T) {
    tests := []struct {
        s string
        ans int
    } {
        // Normal cases
        {"abcabcbb", 3},
        {"pwwkew", 3},
        // Edge cases
        {"", 0},
        {"b", 1},
        {"bbbbbbbb", 1},
        {"abcabcabcd", 4},
        // Chinense cases
        {"这里是慕课网", 6},
        {"一二三二一", 3},
        {"黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花", 8},
    }

    for _, tt := range tests {
        actual := lengthOfNonRepeatingSubStr(tt.s)
        if actual != tt.ans {
            t.Errorf("got %d for input %s; " +
                "expected %d",
                actual, tt.s, tt.ans)
        }
    }
}

func BenchmarkSubstr(b *testing.B) {
    s := "黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花"
    for i := 0; i < 13; i++ {
        s = s + s
    }
    b.Logf("len(s) = %d", len(s))
    ans := 8
    // 防止生成s计算在时间里
    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        actual := lengthOfNonRepeatingSubStr(s)
        if actual != ans {
            b.Errorf("got %d for input %s; " +
                "expected %d",
                actual, s, ans)
        }
    }
}

 

8-3 使用pprof进行性能调优_第7张图片

8-3 使用pprof进行性能调优_第8张图片

因为现在字符串很长,所以decode占用耗时最大

 

总结:

8-3 使用pprof进行性能调优_第9张图片8-3 使用pprof进行性能调优_第10张图片

8-3 使用pprof进行性能调优_第11张图片

你可能感兴趣的:(go)