Skip to content

Latest commit

 

History

History
27 lines (22 loc) · 697 Bytes

File metadata and controls

27 lines (22 loc) · 697 Bytes

在 Golang 中可以使用 time package 中的 Since() 函数来进行时间间隔计算。 再配合 Go 语言的 defer 就可以优雅的实现该功能。

传统方式:

func main() {
    start := time.Now()

    r := new(big.Int)
    fmt.Println(r.Binomial(1000, 10))

    elapsed := time.Since(start)
    log.Printf("Binomial took %s", elapsed)
}

示例的方式:

func BigIntFactorial(x big.Int) *big.Int {
	defer Duration(time.Now(), "IntFactorial")
	...
}

Reference