博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
时间-日期格式化输出,统计程序时间,统计CPU时间
阅读量:673 次
发布时间:2019-03-16

本文共 1252 字,大约阅读时间需要 4 分钟。

#Note 时间经常用于创建图形、文件的命名和打标签;一个format的格式事半功倍;#另外,还常用来统计程序运行时间,决定大致算法取舍#1 输出时间格式import time# 方法1: 年-月-日-时-分-秒x = time.strftime('%Y-%m-%d %H:%M:%S') x2 = time.strftime('第%W周的第%w天, %Y年的第%j天, 英文表达日月 %a %b')x3 = time.strftime('%D')x4 = time.strftime('%c') #一键全打印:星期几、月、日 时 年print(x)print(x2)print(x3)print(x4)# More Details for %xx see: https://docs.python.org/zh-cn/3/library/datetime.html# 方法2: 年-月-日-时-分-秒,类似c#之类占位的高级操作print('{0}-{1}-{2}-{3}-{4}-{5}'.format(*time.localtime()))print('%s-%s-%s-%s-%s-%s'%(time.localtime()[0:6]))#第一个*解包,{i}占位;第二个用类似c语言的格式化输出#2 统计时间def runTest():    x = [i for i in range(10000)]    time.sleep(5)    return  # Method 1: time for program runningstart = time.time()runTest()end = time.time()print('Total time: ',round(start,2), round(end,2), round(end-start,2))  # Method 2: time for CPU runningst2 = time.process_time()runTest()end2 = time.process_time()print('Clock time(CPU time):', st2, end2, end2-st2)#程序运行时间是开始-结束间隔(s),不等于CPU运行时间(睡眠、用户交互等操作上CPU实际闲置着)。CPU主要衡量算力,

result:

2021-03-06 10:36:04

第09周的第6天, 2021年的第065天, 英文表达日月 Sat Mar
03/06/21
Sat Mar 6 10:36:04 2021

2020-9-2-18-46-53

2020-9-2-18-46-53
Total time: 1599043613.45 1599043618.45 5.0
Clock time(CPU time): 0.1875 0.234375 0.046875

Process finished with exit code 0

转载地址:http://ruvqz.baihongyu.com/

你可能感兴趣的文章