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条)

报告相同问题?

悬赏问题

  • ¥15 求会做聚类,TCN的朋友有偿线上指导。以下是目前遇到的问题
  • ¥100 无网格伽辽金方法研究裂纹扩展的程序
  • ¥15 错误于library(org.Hs.eg.db): 不存在叫‘org.Hs.eg.db’这个名称的程序包,如何解决?
  • ¥60 求一个图片处理程序,要求将图像大小跟现实生活中的大小按比例联系起来的
  • ¥50 求一位精通京东相关开发的专家
  • ¥100 求懂行的大ge给小di解答下!
  • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
  • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
  • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
  • ¥100 华为手机私有App后台保活