douzhongjian0752 2016-03-08 15:57
浏览 27


I try to profiling my go library, to find out what is the cause of being so much slower than same thing in c++.

I have simple benchmark

func BenchmarkFile(t *testing.B) {
    tmpFile, err := ioutil.TempFile("", TMP_FILE_PREFIX)

    fw, err := NewFile(tmpFile.Name())
    text := []byte("testing")
    for i := 0; i < b.N; i++ {
        _, err = fw.Write(text)

NewFile return my custom Writer which encodes data to our binary representation, even compress them, and write to file system.

Running go test -bench . -memprofile mem.out -cpuprofile cpu.out I get

BenchmarkFile-16    2000000000           0.20 ns/op
ok      .../writer/iowriter 9.074s

Than analysing it

# go tool pprof cpu.out 
Entering interactive mode (type "help" for commands)
(pprof) top10
930ms of 930ms total (  100%)
      flat  flat%   sum%        cum   cum%
     930ms   100%   100%      930ms   100%  

I even try to write example.go app which is using my writer, and add pprof.StartCPUProfile(f) as is shown in but with same result.

What am I doing wrong, and how can I determine what is bottleneck of my lib? Thank you in advance

  • 写回答

2条回答 默认 最新

  • 普通网友 2016-03-08 16:09

    Ok it's easy, I miss to add binary to go tool pprof, si it has to be

    # go tool pprof write cpu.out 
    Entering interactive mode (type "help" for commands)
    (pprof) top10
    7.02s of 7.38s total (95.12%)
    Dropped 14 nodes (cum <= 0.04s)
    Showing top 10 nodes out of 32 (cum >= 0.19s)
          flat  flat%   sum%        cum   cum%
         6.55s 88.75% 88.75%      6.76s 91.60%  syscall.Syscall

    and when using benchmark tests, binary is created there and using it gives same result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 ios 新安装app收不到fcm推送
  • ¥15 有没有实力的写手?有过成品的优先
  • ¥15 图像信息库的建立与识别
  • ¥15 韩国网站购物,KG支付的支付回调如何解决
  • ¥15 workstation导入ovf文件,报错,怎么解决呢?
  • ¥15 关于#c语言#的问题:构成555单稳态触发器,采用LED指示灯延时时间,对延时时间进行测量并显示(如楼道声控延时灯)需要Proteus仿真图和C语言代码
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,怎样解决呢?
  • ¥50 如何用单纯形法寻优不能精准找不到给定的参数,并联机构误差识别,给定误差有7个?matlab
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,没有XFS,怎样解决呢?
  • ¥15 应用商店如何检测在架应用内容是否违规?