使用python裝飾器計(jì)算函數(shù)運(yùn)行時(shí)間的實(shí)例
裝飾器在python里面有很重要的作用, 如果能夠熟練使用,將會(huì)大大的提高工作效率
今天就來見識(shí)一下 python 裝飾器,到底是怎么工作的。
本文主要是利用python裝飾器計(jì)算函數(shù)運(yùn)行時(shí)間
一些需要精確的計(jì)算函數(shù)運(yùn)行了多久的程序,都可以采用這種方法
#coding:utf-8 import urllib2,re,time,random,os,datetime import HTMLParser import sys reload(sys) sys.setdefaultencoding('utf-8') #計(jì)算時(shí)間函數(shù) def print_run_time(func): def wrapper(*args, **kw): local_time = time.time() func(*args, **kw) print 'current Function [%s] run time is %.2f' % (func.__name__ ,time.time() - local_time) return wrapper class test: def __init__(self): self.url='' #獲取網(wǎng)頁(yè)頁(yè)面內(nèi)容 #即裝飾器不管參數(shù)有多少,都能使用 @print_run_time def get_html(self,url): headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.2; rv:16.0) Gecko/20100101 Firefox/16.0'}#設(shè)置header req = urllib2.Request(url=url,headers=headers) try: html = urllib2.urlopen(req).read().decode('utf-8') html=HTMLParser.HTMLParser().unescape(html)#處理網(wǎng)頁(yè)內(nèi)容, 可以將一些html類型的符號(hào)如" 轉(zhuǎn)換回雙引號(hào) #html = html.decode('utf-8','replace').encode(sys.getfilesystemencoding())#轉(zhuǎn)碼:避免輸出出現(xiàn)亂碼 except urllib2.HTTPError,e: print(2,u"連接頁(yè)面失敗,錯(cuò)誤原因: %s" % e.code) return None except urllib2.URLError,e: if hasattr(e,'reason'): print(2,u"連接頁(yè)面失敗,錯(cuò)誤原因:%s" % e.reason) return None return html #在類的內(nèi)部使用裝飾器 @print_run_time def run(self): self.url='http://www.baidu.com' self.get_html(self.url) print 'end' #在外面直接使用裝飾器 @print_run_time def get_current_dir(spath): #spath=os.getcwd() #spath=os.path.abspath(os.curdir) for schild in os.listdir(spath): schildpath=spath+'/'+schild if os.path.isdir(schildpath): get_current_dir(schildpath) else: print schildpath if __name__ == '__main__': my_test=test() my_test.run() spath=os.path.abspath('.') get_current_dir(spath)
運(yùn)行結(jié)果:
current Function [get_html] run time is 0.29 end current Function [run] run time is 0.29 05.python_study/03.decorator.py current Function [get_current_dir] run time is 0.00
以上這篇使用python裝飾器計(jì)算函數(shù)運(yùn)行時(shí)間的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- python 動(dòng)態(tài)獲取當(dāng)前運(yùn)行的類名和函數(shù)名的方法
- python每隔N秒運(yùn)行指定函數(shù)的方法
- Python裝飾器限制函數(shù)運(yùn)行時(shí)間超時(shí)則退出執(zhí)行
- python實(shí)現(xiàn)在每個(gè)獨(dú)立進(jìn)程中運(yùn)行一個(gè)函數(shù)的方法
- Python中統(tǒng)計(jì)函數(shù)運(yùn)行耗時(shí)的方法
- python獲取當(dāng)前運(yùn)行函數(shù)名稱的方法實(shí)例代碼
- Python使用多進(jìn)程運(yùn)行含有任意個(gè)參數(shù)的函數(shù)
- 為什么在函數(shù)中運(yùn)行的?Python?代碼速度更快?
相關(guān)文章
在Python中使用Mako模版庫(kù)的簡(jiǎn)單教程
這篇文章主要介紹了在Python中使用Mako模版庫(kù)的簡(jiǎn)單教程,包括在Django或者Tornado框架中集成Mako的方法,需要的朋友可以參考下2015-04-04python機(jī)器學(xué)習(xí)GCN圖卷積神經(jīng)網(wǎng)絡(luò)原理解析
這篇文章主要為大家介紹了GCN圖卷積神經(jīng)網(wǎng)絡(luò)原理及代碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python機(jī)器學(xué)習(xí)NLP自然語言處理基本操作精確分詞
本文是Python機(jī)器學(xué)習(xí)NLP自然語言處理系列文章,帶大家開啟一段學(xué)習(xí)自然語言處理 (NLP) 的旅程. 本文主要學(xué)習(xí)NLP自然語言處理基本操作之如何精確分詞2021-09-09Python常見庫(kù)matplotlib學(xué)習(xí)筆記之畫圖文字的中文顯示
在Python中使用matplotlib或者plotnine模塊繪圖時(shí),常常出現(xiàn)圖表中無法正常顯示中文的問題,下面這篇文章主要給大家介紹了關(guān)于Python常見庫(kù)matplotlib學(xué)習(xí)筆記之畫圖文字的中文顯示的相關(guān)資料,需要的朋友可以參考下2023-05-05matplotlib交互式數(shù)據(jù)光標(biāo)mpldatacursor的實(shí)現(xiàn)
這篇文章主要介紹了matplotlib交互式數(shù)據(jù)光標(biāo)mpldatacursor的實(shí)現(xiàn) ,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02python 生成不重復(fù)的隨機(jī)數(shù)的代碼
用的是篩選法,網(wǎng)上有解釋,簡(jiǎn)單的說 就是先隨機(jī)生成一串?dāng)?shù)字,之后用下標(biāo)來判斷這些數(shù)字有沒有重復(fù),重復(fù)的就篩去2011-05-05Tensorflow讀取并輸出已保存模型的權(quán)重?cái)?shù)值方式
今天小編就為大家分享一篇Tensorflow讀取并輸出已保存模型的權(quán)重?cái)?shù)值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看不看2020-01-01