新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解Pythontime模塊
在我們平常的代碼中,經(jīng)常需要和時間打交道。在Python中,與時間處理相關(guān)的模塊有:time、datetime以及calendar。學會計算時間,對程序的調(diào)優(yōu)非常重要,可以在程序中狂打時間戳,來具體判斷程序中哪一塊耗時最多,從而找到程序調(diào)優(yōu)的重心處。這里先來講一個time模塊。

創(chuàng)新互聯(lián)公司主要從事網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)三山,十余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
Python3 Time 模塊介紹Python3 Time 模塊介紹
| 序號 | 函數(shù)及描述 | 實例 |
|---|---|---|
| 1 | time.altzone 返回格林威治西部的夏令時地區(qū)的偏移秒數(shù)。如果該地區(qū)在格林威治東部會返回負值(如西歐,包括英國)。對夏令時啟用地區(qū)才能使用。 | 以下實例展示了 altzone()函數(shù)的使用方法:>>> import time >>> print ("time.altzone %d " % time.altzone) time.altzone -28800 |
| 2 | time.asctime([tupletime]) 接受時間元組并返回一個可讀的形式為”Tue Dec 11 18:07:14 2008″(2008年12月11日 周二18時07分14秒)的24個字符的字符串。 | 以下實例展示了 asctime()函數(shù)的使用方法:>>> import time >>> t = time.localtime() >>> print ("time.asctime(t): %s " % time.asctime(t)) time.asctime(t): Thu Apr 7 10:36:20 2016 |
| 3 | time.clock() 用以浮點數(shù)計算的秒數(shù)返回當前的CPU時間。用來衡量不同程序的耗時,比time.time()更有用。 | 實例由于該方法依賴操作系統(tǒng),在 Python 3.3 以后不被推薦,而在 3.8 版本中被移除,需使用下列兩個函數(shù)替代。time.perf_counter() # 返回系統(tǒng)運行時間 time.process_time() # 返回進程運行時間 |
| 4 | time.ctime([secs]) 作用相當于asctime(localtime(secs)),未給參數(shù)相當于asctime() | 以下實例展示了 ctime()函數(shù)的使用方法:>>> import time >>> print ("time.ctime() : %s" % time.ctime()) time.ctime() : Thu Apr 7 10:51:58 2016 |
| 5 | time.gmtime([secs]) 接收時間戳(1970紀元后經(jīng)過的浮點秒數(shù))并返回格林威治天文時間下的時間元組t。注:t.tm_isdst始終為0 | 以下實例展示了 gmtime()函數(shù)的使用方法:>>> import time >>> print ("gmtime :", time.gmtime(1455508609.34375)) gmtime : time.struct_time(tm_year=2016, tm_mon=2, tm_mday=15, tm_hour=3, tm_min=56, tm_sec=49, tm_wday=0, tm_yday=46, tm_isdst=0) |
| 6 | time.localtime([secs] 接收時間戳(1970紀元后經(jīng)過的浮點秒數(shù))并返回當?shù)貢r間下的時間元組t(t.tm_isdst可取0或1,取決于當?shù)禺敃r是不是夏令時)。 | 以下實例展示了 localtime()函數(shù)的使用方法:>>> import time >>> print ("localtime(): ", time.localtime(1455508609.34375)) localtime(): time.struct_time(tm_year=2016, tm_mon=2, tm_mday=15, tm_hour=11, tm_min=56, tm_sec=49, tm_wday=0, tm_yday=46, tm_isdst=0) |
| 7 | time.mktime(tupletime) 接受時間元組并返回時間戳(1970紀元后經(jīng)過的浮點秒數(shù))。 | 實例 |
| 8 | time.sleep(secs) 推遲調(diào)用線程的運行,secs指秒數(shù)。 | 以下實例展示了 sleep()函數(shù)的使用方法:#!/usr/bin/python3 import time print ("Start : %s" % time.ctime()) time.sleep( 5 ) print ("End : %s" % time.ctime()) |
| 9 | time.strftime(fmt[,tupletime]) 接收以時間元組,并返回以可讀字符串表示的當?shù)貢r間,格式由fmt決定。 | 以下實例展示了 strftime()函數(shù)的使用方法:>>> import time >>> print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) 2016-04-07 11:18:05 |
| 10 | time.strptime(str,fmt=’%a %b %d %H:%M:%S %Y’) 根據(jù)fmt的格式把一個時間字符串解析為時間元組。 | 以下實例展示了 strptime()函數(shù)的使用方法:>>> import time >>> struct_time = time.strptime("30 Nov 00", "%d %b %y") >>> print ("返回元組: ", struct_time) 返回元組: time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1) |
| 11 | time.time( ) 返回當前時間的時間戳(1970紀元后經(jīng)過的浮點秒數(shù))。 | 以下實例展示了 time()函數(shù)的使用方法:>>> import time >>> print(time.time()) 1459999336.1963577 |
| 12 | time.tzset() 根據(jù)環(huán)境變量TZ重新初始化時間相關(guān)設(shè)置。 | 實例 |
| 13 | ** time.perf_counter()** 返回計時器的精準時間(系統(tǒng)的運行時間),包含整個系統(tǒng)的睡眠時間。由于返回值的基準點是未定義的,所以,只有連續(xù)調(diào)用的結(jié)果之間的差才是有效的。 | 實例 |
| 14 | ** time.process_time()** 返回當前進程執(zhí)行 CPU 的時間總和,不包含睡眠時間。由于返回值的基準點是未定義的,所以,只有連續(xù)調(diào)用的結(jié)果之間的差才是有效的。 |
顯示詳細信息
Time模塊包含了以下2個非常重要的屬性:
| 序號 | 屬性及描述 |
|---|---|
| 1 | time.timezone 屬性time.timezone是當?shù)貢r區(qū)(未啟動夏令時)距離格林威治的偏移秒數(shù)(>0,美洲; |
| 2 | time.tzname 屬性time.tzname包含一對根據(jù)情況的不同而不同的字符串,分別是帶夏令時的本地時區(qū)名稱,和不帶的。 |
網(wǎng)頁名稱:詳解Pythontime模塊
鏈接分享:http://m.fisionsoft.com.cn/article/cojioop.html


咨詢
建站咨詢
