dongmeiba6151 2017-03-29 00:04
浏览 125
已采纳

运行Go Benchmark时输出奇怪

I'm trying to write a benchmark function for a simple echo program in Go (Exercise 1.3 from "The Go Programming Language" book). Here is the code:

package echo

import "testing"

func BenchmarkEcho1(b *testing.B) {
    for i := 0; i < b.N; i++ {
        Echo1()
    }
}

When I run go test -bench=. the output is this:

PASS
BenchmarkEcho1-4    -test.bench=Echo1
-test.bench=Echo1
-test.bench=Echo1
-test.bench=Echo1
[...]
-test.bench=Echo1
-test.bench=Echo1
-test.bench=Echo1
-test.bench=Echo1
 1000000          1358 ns/op
ok      gopl.io/ch1/exercise1.3/echo    1.377s

There a lot of -test.bench=Echo1 between the first and the last lines. Why is this happening? What can I do to omit these lines?

  • 写回答

1条回答 默认 最新

  • dongyi2159 2017-03-29 00:29
    关注

    You understand that b.N has a really big value and thus Echo1 gets executed many many times ? The only explanation is that Echo1 is printing the text you are seeing.

    Your Echo1() function probably contains something like this:

    var s string
    for i := 1; i < len(os.Args); i++ {
        s += os.Args[i]
    }
    fmt.Println(s)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备