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

你可能感兴趣的文章
Nacos部署中的一些常见问题汇总
查看>>
NACOS部署,微服务框架之NACOS-单机、集群方式部署
查看>>
Nacos配置Mysql数据库
查看>>
Nacos配置中心中配置文件的创建、微服务读取nacos配置中心
查看>>
Nacos配置中心集群原理及源码分析
查看>>
nacos配置在代码中如何引用
查看>>
nacos配置新增不成功
查看>>
nacos配置自动刷新源码解析
查看>>
nacos集成分布式事务插件Seata的序列化问题,实际上是Seata本身存在bug!!
查看>>
Nacos集群搭建
查看>>
nacos集群搭建
查看>>
nacos集群网络分区对的影响和运维方式
查看>>
nacos集群节点故障对应用的影响以及应急方法
查看>>
nacos集群配置详解
查看>>
Nagios 3.0 Jumpstart Guide For Linux – Overview, Installation and Configuration
查看>>
nagios 实时监控 iptables 状态
查看>>
WAP短信格式解析及在Linux下用C语言实现
查看>>
nagios+cacti整合
查看>>
Nagios介绍
查看>>
nagios利用NSCient监控远程window主机
查看>>