這篇文章主要介紹了詳解Python編程中time模塊的使用,是Python入門學習中的基礎知識,需要的朋友可以參考下
一、簡介
time模塊提供各種操作時間的函數
說明:一般有兩種表示時間的方式:
第一種是時間戳的方式(相對于1970.1.1 00:00:00以秒計算的偏移量),時間戳是惟一的
第二種以數組的形式表示即(struct_time),共有九個元素,分別表示,同一個時間戳的struct_time會因為時區不同而不同
year (four digits, e.g. 1998)
month (1-12)
day (1-31)
hours (0-23)
minutes (0-59)
seconds (0-59)
weekday (0-6, Monday is 0)
Julian day (day in the year, 1-366)
DST (Daylight Savings Time) flag (-1, 0 or 1) 是否是夏令時
If the DST flag is 0, the time is given in the regular time zone;
if it is 1, the time is given in the DST time zone;
if it is -1, mktime() should guess based on the date and time.
夏令時介紹:http://baike.baidu.com/view/100246.htm
UTC介紹:http://wenda.tianya.cn/wenda/thread?tid=283921a9da7c5aef&clk=wttpcts
二、函數介紹
1.asctime()
asctime([tuple]) -> string
將一個struct_time(默認為當時時間),轉換成字符串
Convert a time tuple to a string, e.g. ‘Sat Jun 06 16:26:11 1998'.
When the time tuple is not present, current time as returned by localtime() is used.
2.clock()
clock() -> floating point number
該函數有兩個功能,
在第一次調用的時候,返回的是程序運行的實際時間;
以第二次之后的調用,返回的是自第一次調用后,到這次調用的時間間隔
注:
在Xinux上使用 time.time() 而在windows中使用time.clock()可以得到更高的精度.
Xinux和Win在實現系統時鐘的不同。time.clock()是調用的系統時鐘實現,而兩個平臺又有所不同。
主要問題在于Xinux時鐘切換策略:jiffy的實現,因為內核時鐘的切換不是連續的而是間隔一段時間(一般而言在1ms~10ms之間)之后才變化, 所以如果是在Xinux中的兩次耗時較短的調用,通過time.clock()得到的結果是一樣的。
3.sleep(…)
sleep(seconds)
線程推遲指定的時間運行,經過測試,單位為秒
示例:
- import time
- if __name__ == '__main__':
- time.sleep(3)
- print "clock1: %s" % time.clock()
- # print "local time: %s" % time.localtime()
- print str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
- time.sleep(3)
- print "clock2: %s" % time.clock()
- # print "local time: %s" % time.localtime()
- print str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
- time.sleep(3)
- print "clock3: %s" % time.clock()
- # print "local time: %s" % time.localtime()
- print str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
結果
- clock1: 0.020678
- 2015-08-09 00:18:31
- clock2: 0.020891
- 2015-08-09 00:18:34
- clock3: 0.021068
- 2015-08-09 00:18:37
4.ctime(…)
ctime(seconds) -> string
將一個時間戳(默認為當前時間)轉換成一個時間字符串
例如:
- time.ctime()
輸出為:
- 'Sat Mar 28 22:24:24 2009′
5.gmtime(…)
gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst)
將一個時間戳轉換成一個UTC時區(0時區)的struct_time,如果seconds參數未輸入,則以當前時間為轉換標準
6.localtime(…)
localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)
將一個時間戳轉換成一個當前時區的struct_time,如果seconds參數未輸入,則以當前時間為轉換標準
7.mktime(…)
mktime(tuple) -> floating point number
將一個以struct_time轉換為時間戳
8.strftime(…)
strftime(format[, tuple]) -> string
將指定的struct_time(默認為當前時間),根據指定的格式化字符串輸出
python中時間日期格式化符號:
%y 兩位數的年份表示(00-99)
%Y 四位數的年份表示(000-9999)
%m 月份(01-12)
%d 月內中的一天(0-31)
%H 24小時制小時數(0-23)
%I 12小時制小時數(01-12)
%M 分鐘數(00=59)
%S 秒(00-59)
%a 本地簡化星期名稱
%A 本地完整星期名稱
%b 本地簡化的月份名稱
%B 本地完整的月份名稱
%c 本地相應的日期表示和時間表示
%j 年內的一天(001-366)
%p 本地A.M.或P.M.的等價符
%U 一年中的星期數(00-53)星期天為星期的開始
%w 星期(0-6),星期天為星期的開始
%W 一年中的星期數(00-53)星期一為星期的開始
%x 本地相應的日期表示
%X 本地相應的時間表示
%Z 當前時區的名稱
%% %號本身
- print str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
- 2015-08-09 00:18:37
9.strptime(…)
strptime(string, format) -> struct_time
將時間字符串根據指定的格式化符轉換成數組形式的時間
例如:
2009-03-20 11:45:39 對應的格式化字符串為:%Y-%m-%d %H:%M:%S
Sat Mar 28 22:24:24 2009 對應的格式化字符串為:%a %b %d %H:%M:%S %Y
10.time(…)
time() -> floating point number
返回當前時間的時間戳(1970紀元后經過的浮點秒數)
三、常用命令
1.python獲取當前時間
time.time() 獲取當前時間戳
time.localtime() 當前時間的struct_time形式
time.ctime() 當前時間的字符串形式
- print time.time()
- print time.localtime()
- print time.ctime()
結果為:
- 1439051479.08
- time.struct_time(tm_year=2015, tm_mon=8, tm_mday=9, tm_hour=0, tm_min=31, tm_sec=19, tm_wday=6, tm_yday=221, tm_isdst=0)
- Sun Aug 9 00:31:19 2015
2.python格式化字符串
格式化成2009-03-20 11:45:39形式
- time.strftime(“%Y-%m-%d %H:%M:%S”, time.localtime())
格式化成Sat Mar 28 22:24:24 2009形式
- time.strftime(“%a %b %d %H:%M:%S %Y”, time.localtime())
3.將格式字符串轉換為時間戳
a = “Sat Mar 28 22:24:24 2009″
- b = time.mktime(time.strptime(a,”%a %b %d %H:%M:%S %Y”))
ps:
了解這一塊主要是想用time來計算我程序中關鍵既不的運行時間,所以更多整理這部分內容。至于時間的轉化等,后續用得著的時候再來整理。
四、使用time模塊計算代碼執行效率的精度測試
- #python中使用time模塊計算代碼執行效率
- #測試用time.time()和time.clock()使用精度
- import sys
- import time
- import timeit
- default_timer = None
- if sys.platform == "win32":
- # On Windows, the best timer is time.clock()
- default_timer = time.clock
- else:
- # On most other platforms the best timer is time.time()
- default_timer = time.time
- print default_timer
- timeIn= time.clock()
- for i in range(100):
- n=i
- timeUse = time.clock()-timeIn
- print timeUse
- timeIn = time.time()
- for i in range(100):
- n=i
- timeUse = time.time()-timeIn
- print timeUse
- timeIn = timeit.default_timer()
- for i in range(100):
- n=i
- timeUse = timeit.default_timer()-timeIn
- print timeUse
- #該段代碼在windows下結果如下
- >>>
- 4.07873067161e-005
- 0.0
- 3.5758734839e-005
- #因為time.clock() 返回的是處理器時間,而因為 Unix 中 jiffy 的緣故,所以精度不會太高。
- #因此,在Windows 系統中,建議使用 time.clock(),在Unix 系統中,建議使用 time.time(),
- #而使用timeit代替 time,就可以實現跨平臺的精度性,使用timeit.default_timer()函數來獲取時間
新聞熱點
疑難解答