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 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?