testing - 在 golang 中运行没有 "go test"的基准测试

标签 testing go benchmarking

我想在我的应用程序中运行基准测试。那可能吗?如果可以,我怎么办?我一直在四处寻找,但到目前为止还没有看到任何迹象。

最佳答案

您可以使用 testing.Benchmark无需运行 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
}

关于testing - 在 golang 中运行没有 "go test"的基准测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43402469/

相关文章:

iphone - 如何测试 shouldAutorotateToInterfaceOrientation?

Goroutines 执行后卡住

networking - 如何衡量网络性能(如何对网络协议(protocol)进行基准测试)

benchmarking - D 性能 : union vs @property

ruby-on-rails - 评估其他模型数据时遇到问题

testing - 如何测试延迟的 Go 语句?

testing - 使用 Cypress 执行拖放操作

html - 带有命名参数的 Martini 路由无法加载静态文件

go - 如何在 Go 中获取指向映射中值的指针

cuda - 如何在 CUDA 中使用 64 位指针编写指针追踪基准测试?