duandaiqin6080 2019-08-06 17:27
浏览 90
已采纳

向我解释执行时间的差异

I've started learning Golang with Donovan-Kernighan "The Go Programming Language" book. In chapter 1 the author advises to test several alternative simple echo function implementations. I used time function to calculate execution time like this:

  29 func main() {
  30     var start, end int64
  31     //fmt.Println("Testing echo implementation")
  32     start = time.Now().UTC().UnixNano()
  33     echo3()
  34     end = time.Now().UTC().UnixNano()
  35     fmt.Println(end - start)
  36     start = time.Now().UTC().UnixNano()
  37     echo3()
  38     end = time.Now().UTC().UnixNano()
  39     fmt.Println(end - start)
  40 }

And in case I build code with go build ... command I get the result:

➜  go-hello-world ./echo2
./echo2
81073
./echo2
5591

As you can see the first call require ten times bigger amount of time compare to second one. Note that I call the same echo3 function in both cases. I guessed that the first call of fmt.Println required to much time. For this purpose I've added function call in line 31 and tested again:

➜  go-hello-world ./echo2
Testing echo implementation
./echo2
6327
./echo2
4249

You can see that both calls require similar amount of time.

But where is a source of truth?

Another one interesting moment is that when I run my code by go run ... command I get much smaller amount of time:

➜  go-hello-world go run echo2.go
Testing echo implementation
/tmp/go-build040243191/b001/exe/echo2
1743
/tmp/go-build040243191/b001/exe/echo2
1133
➜  go-hello-world go run echo2.go
/tmp/go-build646239204/b001/exe/echo2
34525
/tmp/go-build646239204/b001/exe/echo2
1133

I thought when I make a binary with build command I get more effective machine code for execution. Could you please explain me why it works in this way in practice.

Environment:

OS: ArchLinux 5.2.5-arch1-1-ARCH
Go: go1.12.7 linux/amd64

P.S. I'm sorry for my English.

UPD#1

echo3 code:

  20 func echo3() {
  21     s, sep := "", ""
  22     for _, arg := range os.Args[:] {
  23         s += sep + arg
  24         sep = " "
  25     }
  26     fmt.Println(s)
  27 }

UPD#2

➜  go-hello-world go run echo2.go
/tmp/go-build573021176/b001/exe/echo2
13019
/tmp/go-build573021176/b001/exe/echo2
1067
/tmp/go-build573021176/b001/exe/echo2
955
/tmp/go-build573021176/b001/exe/echo2
987

and

➜  go-hello-world ./echo2          
./echo2
199681
./echo2
19404
./echo2
5965
./echo2
4803
./echo2
4673
./echo2
8244
  • 写回答

1条回答 默认 最新

  • duan198409 2019-08-06 18:20
    关注

    Try writing a benchmark method with a loop running each method a few hundred times or so using the benchmark techniques shown in section 11.4.

    The discrepancies between runs might be various measurement errors caused by a multitasking operating system and timer resolution and such.

    Also, I think it´s ok to go back to exercises when you are further along (The exercise expects some familiarity with the topic)

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

报告相同问题?

悬赏问题

  • ¥15 qt+ffmpeg报错non-existing PPS 0 referenced
  • ¥15 FOC simulink
  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?