dongtanliefang8765 2019-07-01 08:19
浏览 356

Golang基准测试中的alloc / op零是什么?

Here I have a benchmark test:

// bench_test.go

package main

import (
    "testing"
)

func BenchmarkHello(b *testing.B) {
    for i := 0; i < b.N; i++ {
        a := 1
        a++
    }
}

The test result are strange. The metric allocs/op shows zero. I know variable a is a int type and doesn't take too much memory, but it's not zero.

> go test -bench=. -benchmem
goos: darwin
goarch: amd64
pkg: a
BenchmarkHello-4    2000000000           0.26 ns/op        0 B/op          0 allocs/op
PASS
ok      a   0.553s

Questions

This post points that allocs/op means how many distinct memory allocations occurred per op (single iteration).

  • But what does memory allocation mean? does that mean set a value for a variable? Is it kind ofmalloc in C` ?

  • Why is this metric allocs/ops zero?

  • 写回答

2条回答 默认 最新

  • dqkf36241 2019-07-01 08:24
    关注

    Possible Answer

    From Dave Cheney, one of the Go contributors:

    The benchmark tool only reports heap allocations. Stack allocations via escape analysis are less costly, possibly free, so are not reported.

    Reference

    Why is this simple benchmark showing zero allocations?

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)