27讲 time库

>>> import time
>>> time.time()
1582631472.4291966
>>> time.gmtime()
time.struct_time(tm_year=2020, tm_mon=2, tm_mday=25, tm_hour=11, tm_min=52, tm_sec=18, tm_wday=1, tm_yday=56, tm_isdst=0)
>>> time.ctime()
'Tue Feb 25 19:54:02 2020'
import time
def coreLoop():
    limit=10**8
    while (limit > 0):
        limit -= 1

def otherLoop1():
    time.sleep(0.2)

def otherLoop2():
    time.sleep(0.4)

def main():
    startTime=time.localtime()
    print("程序开Y-%m-%d %H:%M:%S",startTime)
    staetPerfCounter = time.perf_counter()
    otherLoop1()
    otherLoop1PerfCounter=time.perf_counter()
    otherLoop1Perf=otherLoop1PerfCounter-staetPerfCounter
    coreLoop()
    coreLoopPerfCounter=time.perf_counter()
    coreLoopPerf=coreLoopPerfCounter-otherLoop1PerfCounter
    otherLoop2()
    otherLoop2PerfCounter=time.perf_counter()
    otherLoop2Perf=otherLoop2PerfCounter-coreLoopPerfCounter
    endPerfCounter=time.perf_counter()
    totalPerf=endPerfCounter-startPerfCounter
    endTime=time.localtime()
    print("模板1运行时间是:{}秒".format(otherLoop1Perf))
    print("核心模板运行时间是:{}秒".format(coreLoop1Perf))    
    print("模板2运行时间是:{}秒".format(otherLoop2Perf))
    print("程序运行总时间是:{}秒".format(totalPerf))
    print("程序结束时间:",time.strftime("Y-%m-%d %H:%M:%S",endTime))
main()

=============== RESTART: D:\ing\python\python\files\example.py ===============
程序开Y-%m-%d %H:%M:%S time.struct_time(tm_year=2020, tm_mon=2, tm_mday=25, tm_hour=21, tm_min=51, tm_sec=59, tm_wday=1, tm_yday=56, tm_isdst=0)

你可能感兴趣的:(27讲 time库)