天天看点

kobe - 时间,日期,回顾了for循环记录代码运行了多久

# 时间
# 1970年1月1日 到现在一共过了多少秒
# import time
# t1 = time.time()
# print(t1)
# # 睡眠两秒
# time.sleep(2)
# t2 = time.time()
# # print(t2)
# # 本地时间
# localtime = time.localtime()
# print(localtime)
# # year 年 month 月份  day日 16  hour  11点  minute32分钟  second9秒  week of day 6 yday 47 isdst 0
# # 夏令时
# # 转成我们平时看时间的格式
# print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
# # 游戏时间  几点几分
# print(time.strftime("%a %b %H:%M",time.localtime()))
# print(time.strftime("%A %B %H:%M",time.localtime()))

# 日历  calendar
import calendar
# 月日历
cal = calendar.month(202,2)
print(cal)

# 年日历
cal = calendar.calendar(202)
print(cal)

with open("日历.txt", "w") as  f:
    f.write(cal)


           

记录代码运行了多久

import time
# 记录了代码运行了多久
# 计算1-100的和
t1 = time.time()
n=0
for a in range(1000001):
    n=n+a
    print(n)
t2 = time.time()
print("代码运行的时间为%f秒"%(t2-t1))

           

继续阅读