time库的使用

python的time库的功能

  • 计算机时间的表达
  • 提供获取系统时间并格式化输出功能
  • 提供系统级精确计时功能,用于程序性能分析

time库的相关函数

  • 时间获取:time(),ctime,gmtime()
  • 时间格式化:strftime(),strptime()
  • 程序计时:sleep(),perf_counter()

一、时间获取

1、time()
获取当前时间戳,即计算机内部时间只,是一个以秒为单位的浮点数。表示从1970年1月1日0:00开始到当前时刻的时间

import time    #导入time库
print(time.time())

运行结果:

1588050810.4218543

2、ctime()
获取当前时间,并以易读性方式表示,返回字符串

import time
print(time.ctime())

运行结果:

Tue Apr 28 13:15:57 2020

3、gmtime()
获取当前时间,返回一个time.struct_time的类,对于返回的类型,计算机可以对其直接进行处理

import time
print(time.gmtime())
print(type(time.gmtime()))

运行结果:

time.struct_time(tm_year=2020, tm_mon=4, tm_mday=28, tm_hour=5, tm_min=18, tm_sec=32, tm_wday=1, tm_yday=119, tm_isdst=0)
<class 'time.struct_time'>

二、时间格式化
时间格式化类似于字符串格式化,需要有展示模板,展示模板由特定的格式化控制符组成
常见的控制符如下

1、strftime(tql,ts)
tpl是格式化模板字符串,用来定义输出效果。ts是计算机内部时间类型变量

import time
t=time.gmtime()
now1=time.strftime("Now is %Y-%m-%d %H:%M:%S",t)
print(now1)
now2=time.strftime("现在是%Y年%m月%d日 %H时%M分%S秒",t)
print(now2)

运行结果:

Now is 2020-04-28 05:38:03
现在是2020年04月28日 05时38分03秒

2、strptime(str,tpl)
strptime()和strftime()互补,可以将字符串类型的时间数据读取.str是字符形式的时间值,tpl是格式化模板字符串,用来定义输入效果

import time
str="2020年4月28日13时42分10秒"
st=time.strptime(str,"%Y年%m月%d日%H时%M分%S秒")
str=time.strftime("%Y年%m月%d日 %H时%M分%S秒",st)
print(str)
2020年04月28日 13时42分10秒

三、程序计时
程序计时指测量起止动过所经历的时间过程

1、perf_counter()
返回一个CPU级别的精确时间计数值,单位为秒。由于这个数值的不确定性,连续调用差值才有意义

import time
start=time.perf_counter()
for i in range(10):
    print("hello world!")
end=time.perf_counter()
t=end-start
print(f'运行所需时间为{t}秒')
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
运行所需时间为0.03368020989000797秒

2、sleep(s)
s为指定的休眠时间,可以是浮点数

import time
start=time.perf_counter()
for i in range(10):
    print("hello world!")
    time.sleep(1)   #每打印一个"hello world"就休眠1秒
end=time.perf_counter()
t=end-start
print(f'运行所需时间为{t}秒')

运行结果:

hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
运行所需时间为10.013963906094432秒
1 按照某种格式输出系统时间:自学java中的时间。自学Date类(java.util.Date)和SimpleDateFormat类,用程序输出“当前时间是XXXX年XX月XX日XX时XX分XX秒(第XX周,周X)” 2 比较String与StringBuffer、StringBuilder的自增速度。方法:在自增前记录系统时间,进行5000次自增,然后再记录系统时间,可以得出自增的速度。获取系统时间的方法是java.lang.System.currentTimeMillis() ,见JDK API。 3 定义类,圆是由圆心()和半径组成的,请完成圆类,注意“派”为常量,取3.14。圆类具有judge函数可以判断两圆的关系:相离、相交(包括相切)、嵌套、重合。设计static成员用以求所有圆对象的平均面积。(static、final) 4 图形间的关系可以下图用来表现。所有的图形中都可以称为Shape。由这个类可以派生出二维图形TwoDimensionalShape和三维图形ThreeDimensionalShape类。每个TwoDimensionalShape类应包括成员函数getArea以计算二维图形的面积。每个ThreeDimensionalShape类包含成员函数getArea和getVolume,分别计算三维图形的表面积和体积。编写一个程序,用一个数组乘放各种图形类对象(包括二维和三维的),并输出对象得相应信息。如果某个图形是TwoDimensionalShape就显示其面积,如果某个图形是ThreeDimenionalShape,则显示其面积和体积。请完成图中所有的类并在主函数中做测试。(继承与多态)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值