博客
关于我
时间-日期格式化输出,统计程序时间,统计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/

你可能感兴趣的文章
mysql deadlock found when trying to get lock暴力解决
查看>>
MuseTalk如何生成高质量视频(使用技巧)
查看>>
mutiplemap 总结
查看>>
MySQL DELETE 表别名问题
查看>>
MySQL Error Handling in Stored Procedures---转载
查看>>
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
MySQL InnoDB 三大文件日志,看完秒懂
查看>>
Mysql InnoDB 数据更新导致锁表
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>
MySQL InnoDB事务隔离级别与锁机制深入解析
查看>>
Mysql InnoDB存储引擎 —— 数据页
查看>>
Mysql InnoDB存储引擎中的checkpoint技术
查看>>
Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
查看>>