我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

在golang中运行基准而无需“进行测试”

I want to run the benchmarks in my application. Is that possible? If so, how can I? I have been looking around but haven't seen any indication so far.

You can use testing.Benchmark without running go test.

package main

import (
    "fmt"
    "testing"
)

func Fib(n int) int {
    if n <= 0 {
        return 0
    } else if n == 1 {
        return 1
    }
    return Fib(n-1) + Fib(n-2)
}

func main() {
    res := testing.Benchmark(func(b *testing.B) {
        for n := 0; n < b.N; n++ {
            Fib(10)
        }
    })
    fmt.Println(res)
    // (on my mac)
    // 3000000               454 ns/op
}

近期文章

  • 这两个变体之间有什么区别?
  • Go函数类型,该函数类型返回与接口一起使用的结构
  • 如何在不使用go get的情况下安装Go应用?
  • 使用接受io.Writer作为HandleFunc的函数
  • 如何通过其字符串名称和字符串结构名称调用方法?
  • Go加密与使用相同密钥和iv的Ruby加密不同
  • 在模板中,我如何拥有一个柜台?
  • 如何使用RSA加密大文件?
  • 如何使用net / http打破大块?
  • Golang-为什么SQL占位符不起作用?
  • 如何在Go中创建可变类型的切片?
  • 使用Golang SDK的S3对象不会过期
  • 在Go Lang中处理Aerospike库错误
  • Go和Java关于接口的区别是什么? [关闭]
  • 如何包装交互式命令
  • 将go嵌入式结构传递给函数
  • Goroutine不使用最大CPU和内核
  • 如何在以下代码中实现接口?
  • 如何在Golang中设置Redis密钥的到期时间
  • Golang中的文件路径

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华