加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang 使用pprof进行性能调优

发布时间:2020-12-16 09:28:35 所属栏目:大数据 来源:网络整理
导读:? package mainimport "fmt"func lengthOfNonRepeatingSubStr(s string) int {lastOccurred := make(map[rune]int)start := 0maxLength := 0for i,ch := range []rune(s) {if lastI,ok := lastOccurred[ch]; ok lastI = start {start = lastI + 1}if i-start

?

package main

import "fmt"

func lengthOfNonRepeatingSubStr(s string) int {
	lastOccurred := make(map[rune]int)
	start := 0
	maxLength := 0
	for i,ch := range []rune(s) {
		if lastI,ok := lastOccurred[ch]; ok && lastI >= start {
			start = lastI + 1
		}
		if i-start+1 > maxLength {
			maxLength = i - start + 1
		}
		lastOccurred[ch] = i
	}
	return maxLength
}

func main() {
	fmt.Println(lengthOfNonRepeatingSubStr("123123"))
}

  随便写一个名字叫nonrepeat.go的文件,然后再写了一个nonrepeat_test.go

package main

import "testing"

func BenchmarkLengthOfNonRepeatingSubStr(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if lengthOfNonRepeatingSubStr("123123") != 3 {
			b.Errorf("正确的值是:%d",3)
		}
	}
}

  然后执行:

go test -bench .  -cpuprofile cpu.out
goos: darwin
goarch: amd64
pkg: gopcp.v2/chapter7/nonrepeat
BenchmarkLengthOfNonRepeatingSubStr-4           10000000               225 ns/op
PASS
ok      gopcp.v2/chapter7/nonrepeat     2.646s

  

 nonrepeat go tool pprof cpu.out 
Type: cpu
Time: Apr 16,2019 at 6:53pm (CST)
Duration: 2.64s,Total samples = 2.28s (86.48%)
Entering interactive mode (type "help" for commands,"o" for options)
(pprof) 

  mac 上面还需要安装图形化的界面工具?https://www.macports.org/install.php?,实在不行参考?https://blog.csdn.net/qq_36847641/article/details/78224910?这个安装盒子

  

(pprof) web
Failed to execute dot. Is Graphviz installed? Error: exec: "dot": executable file not found in $PATH
(pprof) 

  

上面分析得出map 访问占用的性能比较高,可以换个用 slice 处理

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读