博客
关于我
时间-日期格式化输出,统计程序时间,统计CPU时间
阅读量:677 次
发布时间: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/

你可能感兴趣的文章
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>