python学习 第四个程序 标准库time的使用

# GetTime.py
import time
'''
标准库time的使用
时间获取
time() unix时间戳, 单位为秒
ctime() 本地时间字符串
gmtime() 格林威治时间,+8h为北京时间
时间格式化
strftime(formatStr, t)
strptime(timeStr, formatStr) 返回格林威治时间
计时
perf_counter() 返回浮点数,单位为秒,用于计算时间差
sleep(secs) 休眠s秒
'''

t1 = time.time()
print(t1)
t2 = time.ctime()
print(t2)
t3 = time.gmtime()
print(t3)
strTime = time.strftime("%Y-%m-%d %H:%M:%S", t3)
print(strTime)

time.sleep(2)
print("2s过去了")

num = 0
start = time.perf_counter()
for i in range(10000000):
num += i
end = time.perf_counter()
print("循环用了{:.3f}秒".format(end-start))



猜你喜欢

转载自www.cnblogs.com/wumingoo1/p/10319046.html