Python如何获得程序运行时间的格式化显示
发布网友
发布时间:2022-04-21 03:04
我来回答
共1个回答
热心网友
时间:2022-04-18 03:03
(1)在程序启动时获得当前时间:
recordTime = time.time()
(2)同时获得当前时间的格式化串:
startTime = time.strftime("%H%M%S")
(3)在主循环中按秒进行判断:
timeGap = time.time() - recordTime
if timeGap >= 1: #这是按1秒设置的,可以根据实际需要设置
recordTime += timeGap
showTime_String = get_lapseTime(startTime, time.strftime("%H%M%S"))
(4)函数:
def get_lapseTime(aTime, bTime):
aNum = 3600 * int(aTime[:2]) + 60 * int(aTime[2:4]) + int(aTime[-2:])
bNum = 3600 * int(bTime[:2]) + 60 * int(bTime[2:4]) + int(bTime[-2:])
gapH = (bNum - aNum) // 3600
gapM = ((bNum - aNum) % 3600) // 60
gapS = ((bNum - aNum) % 3600) % 60
gapTime = "%02d:%02d:%02d"%(gapH,gapM,gapS)
return(gapTime)