利用Python的time库函数实现程序计时功能

import time
def coreLoop():
    limit=10**8
    while (limit>0):
        limit-=1
def otherLoop1():
    time.sleep(0.2)
def otherLoop2():
    time.sleep(0.4)
def main():
    starttime=time.localtime()
    print('程序开始时间:',time.strftime('%Y-%m-%d-%H:%M:%S',starttime))
    startPerfCounter=time.perf_counter()
    otherLoop1()
    otherLoop1PerfCounter=time.perf_counter()
    otherLoop1Perf=otherLoop1PerfCounter-startPerfCounter
    coreLoop()
    coreLoopPerfCounter=time.perf_counter()
    coreLoopPerf=coreLoopPerfCounter-otherLoop1PerfCounter
    otherLoop2()
    otherLoop2PerfCounter=time.perf_counter()
    otherLoop2Perf=otherLoop2PerfCounter-coreLoopPerfCounter
    endPerfCounter=time.perf_counter()
    totalPerf=endPerfCounter-startPerfCounter
    endtime=time.localtime()
    print('模块1运行时间是:{}秒'.format(otherLoop1Perf))
    print('核心模块运行时间是:{}秒'.format(coreLoopPerf))
    print('模块2运行时间是:{}秒'.format(otherLoop2Perf))
    print('程序运行结束时间:',time.strftime('%Y-%m-%d-%H:%M:%S',endtime))
main()

运行结果;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pythontime提供了一些常用的时间函数,包括time.time()、time.strftime()和time.strptime()。 time.time()函数返回当前时间的时间戳,以浮点数类型表示。例如,time.time()的返回值可以是1548826080.93。 time.strftime()函数返回一个指定格式的时间字符串。该函数接受两个参数,第一个参数是时间格式,用来指定输出时间字符串的形式,第二个参数是一个时间结构体,用来指定时间。例如,time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())会返回形如"2019-01-30 14:00:33"的时间字符串。 time.strptime()函数将一个时间字符串解析为一个时间结构化的元组。该函数接受两个参数,第一个参数是要解析的时间字符串,第二个参数是时间格式,用来指定输入时间字符串的形式。例如,time.strptime("2019-01-30","%Y-%m-%d")会返回一个元组,其中包含了年、月、日等时间信息。 总结来说,pythontime函数包括time.time()、time.strftime()和time.strptime(),分别用于获取当前时间戳、格式化时间字符串和解析时间字符串。使用这些函数可以方便地处理时间相关的操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Pythontime的一些重要函数介绍](https://blog.csdn.net/weixin_42883530/article/details/86701845)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值