• 技术文章 >常见问题 >Python常见问题

    python怎么设置计时器

    yangyang2020-05-13 10:28:17原创9329

    python设置计时器的方法:

    1、使用time.time()获取开始与结束的时间,将两个时间相减即可进行计时

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    import time

      

      

    time_start = time.time() #开始计时

      

    #要执行的代码,或函数

    #要执行的代码,或函数

      

      

    time_end = time.time()    #结束计时

      

      

    time_c= time_end - time_start   #运行所花时间

    print('time cost', time_c, 's')

    2、在while循环中计算总花费时间

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    import time

      

      

    #初始化之类的

    sum_t=0.0            #花费的总时间

      

      

    while (True):

        time_start = time.time() #开始计时

      

        #要执行的代码,或函数

        #要执行的代码,或函数

      

         

        time_end = time.time()    #结束计时

      

        sum_t=(time_end - time_start)+sum_t   #运行所花时间

        print('time cost', sum_t, 's')

    更多Python知识请关注Python视频教程栏目。

    专题推荐:python
    上一篇:python怎么设置小数点后保留两位小数点 下一篇:python怎么删除字符串最后一个字符?

    相关文章推荐

    • python中画图出现中文乱码怎么解决• python中等号是什么意思?• python找出几个数最大值的方法• python怎么让代码无效

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网