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 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题