python運行加速的幾種方式
1、使用pypy2、減少函數(shù)化調(diào)用3、減少文件的打開即with的調(diào)用,將這一調(diào)用放在for循環(huán)前面,然后傳遞至后面需要用到的地方4、if函數(shù)判斷條件多的盡量在前面全面加速(pypy)
二、全面加速(pypy)將python換為pypy,在純python代碼下,pypy的兼容性就不影響使用了,因為一些純python的代碼常常會用pypy進行一下加速
測試代碼,for循環(huán)10000000次
start = time.time()for i in range(10000000): print(i,end='r')end = time.time()print(f'耗費時間{end-start}秒>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
pypy的耗時為:
而python耗時為
大致三倍,但是循環(huán)越多估計越快,據(jù)說有6倍左右
二、減少文件的打開即with的調(diào)用原代碼的with在調(diào)用函數(shù)內(nèi),即每次調(diào)用函數(shù)都要打開并關(guān)閉文件,造成大量耗時
def BMES(word,tag): with open(r'J:PyCharm項目學(xué)習(xí)進行中NLP教程NLP教程數(shù)據(jù)集詞性標(biāo)注nature2ner.txt','a+',encoding='utf-8')as f_:if len(word) == 1: '''單字''' f_.write(word + ' ' + f'S-{tag.upper()}' + 'n')else: '''多字''' for index, word_ in enumerate(word):if index == 0: f_.write(word_ + ' ' + f'B-{tag.upper()}' + 'n')elif 0 < index < len(word) - 1: f_.write(word_ + ' ' + f'M-{tag.upper()}' + 'n')else: f_.write(word_ + ' ' + f'E-{tag.upper()}' + 'n')#后續(xù)在多個if-elif-else中調(diào)用
耗時為
tqdm預(yù)估時間在15~25個小時左右跳動
將with放在循環(huán)前面
如
將with的內(nèi)容作為f_傳遞進來
后的耗時為:
測試如下:
import os, warnings,time,tqdmdef txt(word): with open('ceshi.txt','a+',encoding='utf-8')as f:if len(str(word))<=2: word+=100 f.write(str(word)+'n')elif 2<len(str(word))<=4: word+=200 f.write(str(word)+'n')else: f.write(str(word) + 'n')if __name__=='__main__': start = time.time() for i in tqdm.tqdm(range(100000)):txt(i) end = time.time() print(f'耗費時間{end-start}秒>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
耗時結(jié)果為:
將文件的打開即with的調(diào)用放在外面
import os, warnings,time,tqdmdef txt(f,word):if len(str(word))<=2: word+=100 f.write(str(word)+'n')elif 2<len(str(word))<=4: word+=200 f.write(str(word)+'n')else: f.write(str(word) + 'n')if __name__=='__main__': start = time.time() with open('ceshi.txt', 'a+', encoding='utf-8')as f:for i in tqdm.tqdm(range(100000)): txt(f,i) end = time.time() print(f'耗費時間{end-start}秒>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
耗時為
結(jié)論:快了119倍,而實際加速遠遠大于這個倍數(shù)
三、if判斷靠前如:
if tag in ['nts', 'nto', 'ntc', 'ntcb', 'ntcf', 'ntch', 'nth', 'ntu', 'nt']:BMES(f_,i2, tag='ORG') elif tag in ['nb', 'nba', 'nbc', 'nbp', 'nf', 'nm', 'nmc', 'nhm', 'nh']:BMES(f_,i2, tag='OBJ') elif tag in ['nnd', 'nnt', 'nn']:BMES(f_,i2, tag='JOB') elif tag in ['nr', 'nrf']:BMES(f_,i2, tag='PER') elif tag in ['t']:BMES(f_,i2, tag='TIME') elif tag in ['ns', 'nsf']:BMES(f_,i2, tag='LOC') else:for i3 in list(i2): f_.write(i3 + ' ' + f'O' + 'n')
滿足條件的可以先跳出判斷
到此這篇關(guān)于python運行加速的幾種方式的文章就介紹到這了,更多相關(guān)python運行加速的幾種方式內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 使用.net core 自帶DI框架實現(xiàn)延遲加載功能2. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究3. Angular獲取ngIf渲染的Dom元素示例4. php面向?qū)ο蟪绦蛟O(shè)計介紹5. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp6. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁7. 三個不常見的 HTML5 實用新特性簡介8. php測試程序運行速度和頁面執(zhí)行速度的代碼9. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報錯問題分析10. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過程解析
