douchuilai2355 2019-02-21 12:00
浏览 46
已采纳

有什么更有效的方法,可以返回指向uint或uint的指针?

In Go, what is more efficient to return from a function: returning a uint or returning a *uint?

The function is called in the inner for-loop of a cpu-intensive library.

  • 写回答

1条回答 默认 最新

  • dongqiao6445 2019-02-21 12:08
    关注

    In general, whenever efficiency is the question, you should run benchmarks.

    Let's create very simple examples:

    func fuint() uint {
        return 0
    }
    
    func fpuint() *uint {
        var i uint
        return &i
    }
    

    And the benchmarking code:

    func BenchmarkUint(b *testing.B) {
        for i := 0; i < b.N; i++ {
            fuint()
        }
    }
    
    func BenchmarkPuint(b *testing.B) {
        for i := 0; i < b.N; i++ {
            fpuint()
        }
    }
    

    One thing to look out for: since we have very simple functions, code optimization might render the result useless (e.g. we might see the same result).

    So instead we should test by disabling compiler optimizations. So let's test it like this:

    go test -gcflags '-N -l' -bench . -benchmem
    

    The test results:

    BenchmarkUint-4     500000000        3.21 ns/op      0 B/op     0 allocs/op
    BenchmarkPuint-4    100000000       22.4 ns/op       8 B/op     1 allocs/op
    

    Just as we could have guessed: allocating an uint value and returning a pointer to it is slower and requires an allocation (on the heap), while returning a simple uint value does not.

    But don't think this is always the case. During this test, we disabled compiler optimizations. When you compile your app to production, you will obviously not disable optimizations, so your real-life examples might not have such big difference, or no difference at all. Test / benchmark your actual code to see if returning a pointer actually makes any noticeable difference.

    For reference, here is the result with compiler optimizations enabled (default behavior):

    go test -bench . -benchmem
    
    BenchmarkUint-4     2000000000         0.35 ns/op        0 B/op      0 allocs/op
    BenchmarkPuint-4    2000000000         0.35 ns/op        0 B/op      0 allocs/op
    

    We see no difference for fuint() and fpuint() at all in the specific examples above.

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

报告相同问题?

悬赏问题

  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥50 vue router 动态路由问题