douba8758 2017-06-20 08:01
浏览 44
已采纳

使用golang实现python的计时器[关闭]

Python:

with Timer() as t :
          // TODO a lot

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!!!

  • 写回答

2条回答 默认 最新

  • douhan9191 2017-06-20 08:05
    关注

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

    import (
      "time"
      "fmt"
    )
    
    func TimeIt(f func(), description string) {
       start := time.Now()
       f()
       fmt.Printf("Running %s took %v
    ", description, time.Since(start))
    }
    

    And then trigger it like so:

    func main() {
        TimeIt(doSomething, "Doing something")
    }
    

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

    TimeIt(func() {
         do()
         some()
         stuff()
       },
       "whatever")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?