博客
关于我
时间-日期格式化输出,统计程序时间,统计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配置文件my.ini详解
查看>>
MySQL配置文件深度解析:10个关键参数及优化技巧---强烈要求的福利来咯。
查看>>
Mysql配置表名忽略大小写(SpringBoot连接表时提示不存在,实际是存在的)
查看>>
mysql配置读写分离并在若依框架使用读写分离
查看>>
MySQL里为什么会建议不要使用SELECT *?
查看>>
MySQL里的那些日志们
查看>>
mysql重新安装?忘记root密码?重装Windows、Linux系统导致mysql没法用吗? 这里有你想要的答案
查看>>
mysql重置root密码
查看>>
MySQL锁
查看>>
MySQL锁与脏读、不可重复读、幻读详解
查看>>
MySQL锁机制
查看>>
mysql锁机制,主从复制
查看>>
Mysql锁机制,行锁表锁
查看>>
MySQL锁表问题排查
查看>>
Mysql锁(1):锁概述和全局锁的介绍
查看>>
Mysql锁(2):表级锁
查看>>
MySQL锁,锁的到底是什么?
查看>>
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
查看>>
Mysql错误2003 -Can't connect toMySQL server on 'localhost'(10061)解决办法
查看>>
MySQL错误提示mysql Statement violates GTID consistency
查看>>