Use golang to achieve python's timer

huangapple go评论114阅读模式
英文:

Use golang to achieve python's timer

问题

Python:

  1. with Timer() as t:
  2. # TODO a lot
  3. print "scan all disks, cost: %s secs" % t.secs

现在,如何使用 golang 实现这个功能?

我已经在谷歌上搜索了,但是没有找到我想要的答案。

为什么我在这里发布问题然后被踩票?

谢谢你的帮助!

英文:

Python:

  1. with Timer() as t :
  2. // TODO a lot
  3. print "scan all disks,cost:%s secs" % t.secs

Now,how to use golang to achieve this?

I had google this but I can not find any answers I want.

Why am I post my questions in here and then got downvote?

Thank you your help!!!

答案1

得分: 4

你可以编写一个函数,接受一个函数作为参数,运行它并打印时间:

  1. import (
  2. "time"
  3. "fmt"
  4. )
  5. func TimeIt(f func(), description string) {
  6. start := time.Now()
  7. f()
  8. fmt.Printf("运行 %s 花费了 %v\n", description, time.Since(start))
  9. }

然后可以这样触发它:

  1. func main() {
  2. TimeIt(doSomething, "做某事")
  3. }

或者将其作为闭包直接应用于代码片段:

  1. TimeIt(func() {
  2. do()
  3. some()
  4. stuff()
  5. },
  6. "任何事情")
英文:

You can write a function that accepts a function, runs it and prints the time:

  1. import (
  2. "time"
  3. "fmt"
  4. )
  5. func TimeIt(f func(), description string) {
  6. start := time.Now()
  7. f()
  8. fmt.Printf("Running %s took %v\n", description, time.Since(start))
  9. }

And then trigger it like so:

  1. func main() {
  2. TimeIt(doSomething, "Doing something")
  3. }

Or just sprinkle it on pieces of code as a closure:

  1. TimeIt(func() {
  2. do()
  3. some()
  4. stuff()
  5. },
  6. "whatever")

答案2

得分: 3

在Go语言中,另一种方法是定义一个计时器函数,并使用defer语句在函数返回时调用它。

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func timer(start time.Time, description string) {
  7. fmt.Printf("%s 耗时 %s\n", description, time.Since(start))
  8. }
  9. func main() {
  10. func() {
  11. defer timer(time.Now(), "扫描所有磁盘")
  12. // TODO: 很多操作
  13. }()
  14. // 输出: 扫描所有磁盘 耗时 0s
  15. }

你可以通过将特定命令包装到一个匿名函数中(参见上面的示例)来测量其执行时间,或者通过在函数顶部简单地放置defer语句来测量函数的执行时间,例如:

  1. func foo() {
  2. defer timer(time.Now(), "foo")
  3. // ...
  4. }
  5. // 输出: foo 耗时 0s
英文:

Another way to do this in go is to define a timer function and use the defer statement to call it when the function returns.

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func timer(start time.Time, description string) {
  7. fmt.Printf("%s took %s\n", description, time.Since(start))
  8. }
  9. func main() {
  10. func() {
  11. defer timer(time.Now(), "scan all disks")
  12. // TODO: alot
  13. }()
  14. // OUTPUT: scan all disks took 0s
  15. }

You can measure the time of specific commands by wrapping them into an anonymous function (see above) or you measure the time of a function by simply putting the defer statement at its top, e.g.

  1. func foo() {
  2. defer timer(time.Now(), "foo")
  3. // ...
  4. }
  5. // OUTPUT: foo took 0s

huangapple
  • 本文由 发表于 2017年6月20日 16:01:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/44647225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定