国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

詳解Python編程中time模塊的使用

2020-01-04 17:55:54
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了詳解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)

線程推遲指定的時間運行,經過測試,單位為秒

示例:

 

 
  1. import time 
  2. if __name__ == '__main__'
  3. time.sleep(3) 
  4. print "clock1: %s" % time.clock() 
  5. # print "local time: %s" % time.localtime() 
  6. print str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())) 
  7. time.sleep(3) 
  8. print "clock2: %s" % time.clock() 
  9. # print "local time: %s" % time.localtime() 
  10. print str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())) 
  11. time.sleep(3) 
  12. print "clock3: %s" % time.clock() 
  13. # print "local time: %s" % time.localtime() 
  14. print str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())) 

結果

 

 
  1. clock1: 0.020678 
  2. 2015-08-09 00:18:31 
  3. clock2: 0.020891 
  4. 2015-08-09 00:18:34 
  5. clock3: 0.021068 
  6. 2015-08-09 00:18:37 

4.ctime(…)

ctime(seconds) -> string

將一個時間戳(默認為當前時間)轉換成一個時間字符串

例如:

 

 
  1. time.ctime() 

輸出為:

 

 
  1. '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 當前時區的名稱

%% %號本身

 

 
  1. print str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())) 

 

 
  1. 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() 當前時間的字符串形式

 

 
  1. print time.time() 
  2. print time.localtime() 
  3. print time.ctime() 

結果為:

 

 
  1. 1439051479.08 
  2. 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) 
  3. Sun Aug 9 00:31:19 2015 

2.python格式化字符串

格式化成2009-03-20 11:45:39形式

 

 
  1. time.strftime(“%Y-%m-%d %H:%M:%S”, time.localtime()) 

格式化成Sat Mar 28 22:24:24 2009形式

 

 
  1. time.strftime(“%a %b %d %H:%M:%S %Y”, time.localtime()) 

3.將格式字符串轉換為時間戳

a = “Sat Mar 28 22:24:24 2009″

 

 
  1. b = time.mktime(time.strptime(a,”%a %b %d %H:%M:%S %Y”)) 

ps:

了解這一塊主要是想用time來計算我程序中關鍵既不的運行時間,所以更多整理這部分內容。至于時間的轉化等,后續用得著的時候再來整理。

四、使用time模塊計算代碼執行效率的精度測試

 

 
  1. #python中使用time模塊計算代碼執行效率  
  2. #測試用time.time()和time.clock()使用精度  
  3.  
  4. import sys  
  5. import time  
  6. import timeit  
  7. default_timer = None 
  8.  
  9. if sys.platform == "win32":  
  10. # On Windows, the best timer is time.clock()  
  11. default_timer = time.clock  
  12. else:  
  13. # On most other platforms the best timer is time.time()  
  14. default_timer = time.time  
  15. print default_timer  
  16. timeIn= time.clock()  
  17. for i in range(100):  
  18. n=i  
  19. timeUse = time.clock()-timeIn  
  20. print timeUse  
  21.  
  22. timeIn = time.time()  
  23. for i in range(100):  
  24. n=i  
  25. timeUse = time.time()-timeIn  
  26. print timeUse  
  27.  
  28. timeIn = timeit.default_timer()  
  29. for i in range(100):  
  30. n=i  
  31. timeUse = timeit.default_timer()-timeIn  
  32. print timeUse  
  33.  
  34.  
  35.  
  36. #該段代碼在windows下結果如下  
  37. >>>  
  38. 4.07873067161e-005 
  39. 0.0 
  40. 3.5758734839e-005 
  41.  
  42. #因為time.clock() 返回的是處理器時間,而因為 Unix 中 jiffy 的緣故,所以精度不會太高。  
  43. #因此,在Windows 系統中,建議使用 time.clock(),在Unix 系統中,建議使用 time.time(),  
  44. #而使用timeit代替 time,就可以實現跨平臺的精度性,使用timeit.default_timer()函數來獲取時間 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武夷山市| 台湾省| 乌兰浩特市| 奈曼旗| 亚东县| 交口县| 杭州市| 讷河市| 长武县| 抚宁县| 武宣县| 酒泉市| 吕梁市| 苏尼特右旗| 阳西县| 理塘县| 虹口区| 汝州市| 化隆| 小金县| 湘乡市| 应用必备| 安丘市| 阳江市| 镇安县| 福建省| 镇康县| 海伦市| 房产| 张家港市| 淮北市| 常山县| 南陵县| 炉霍县| 广德县| 贵州省| 古蔺县| 和静县| 唐海县| 连南| 江华|