Golang 获得一个 Action 的处理时间

标签 go

我真的很想知道花多少时间来做这个 Action 。有人有什么最好的主意吗?或任何图书馆。我用简单的方法做了,但它似乎是错误的方法。因为我确实有更多在 go

上工作的经验

我有那个代码:

package main

import (
    "fmt"
    "time"
)

func main() {
    var d int = 0
    var beginTime = time.Now()
    for i := 0; i < 100000; i++ {

        for c := 0; c < 100; c++ {
            d = 1
        }
    }
    var endTime = time.Now()
    fmt.Println(beginTime, endTime)
    fmt.Println(time.Since(beginTime))
    fmt.Println(d)
}

最佳答案

使用 built-in Benchmarks .

A sample benchmark function looks like this:

func BenchmarkHello(b *testing.B) {
    for i := 0; i < b.N; i++ {
        fmt.Sprintf("hello")
    }
}

The benchmark function must run the target code b.N times. During benchark execution, b.N is adjusted until the benchmark function lasts long enough to be timed reliably. The output

BenchmarkHello    10000000    282 ns/op

means that the loop ran 10000000 times at a speed of 282 ns per loop.

编辑:如果您想在运行时而不是在测试中测量做某事的时间,我认为您的方式很好。

关于Golang 获得一个 Action 的处理时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30098196/

相关文章:

regex - 错误解析正则表达式 : invalid or unsupported Perl syntax: `(?!`

go - 在 Go 中设置引用

intellij-idea - 在 intellij 中调试 golang - 找不到相关文件

go - Go,FreeBSD,iovec和syscalls

eclipse - 如何在eclipse IDE中执行go test文件

golang 惯用的方式来停止 for

function - 去旅游 #18 : understanding pic. 秀

go - 如何在除 Godeps 和 .git 之外的所有文件夹上运行 goimports?

类型别名的 Go 类型开关

go - 切换语句以将特定变量添加到api请求