Python 性能優(yōu)化常見技巧有:改進(jìn)算法選擇合適的數(shù)據(jù)結(jié)構(gòu);并行編程;減少冗余數(shù)據(jù);循環(huán)優(yōu)化;字符串的優(yōu)化;函數(shù)和模塊優(yōu)化;表達(dá)式優(yōu)化等。根據(jù) 80/20 原則,實(shí)現(xiàn)程序的重構(gòu)、優(yōu)化、擴(kuò)展以及文檔相關(guān)的事情通常需要消耗 80% 的工作量。
Python應(yīng)用也有許多優(yōu)化措施,如使用異步、理解性能測(cè)試工具,以及使用多解釋器等。
性能分析主要分為兩類:基于事件的性能分析(event-based profiling)和統(tǒng)計(jì)式的性能分析(statistical profiling)。優(yōu)化通常包含兩方面的內(nèi)容:減小代碼的體積,提高代碼的運(yùn)行效率。Python中最常用的性能分析工具主要有:cProfiler, line_profiler以及memory_profiler等。
使用python時(shí),要養(yǎng)成使用 os.path.join 的習(xí)慣
pipenv shell
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple joblib
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple flask
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple MarkupSafe
pyinstaller -D -w kyj050.py
對(duì)應(yīng)依賴比較多的程序,建議使用 -D, -F更適合單文件的py腳本
pyinstaller -D -w predict_rec.py
RecursionError: maximum recursion depth exceeded
在*.spec文件中增加兩行
import sys
sys.setrecursionlimit(100000)
pyinstaller predict_rec.spec
pyinstaller recc.spec
Pyinstaller UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position解決方案
chcp 65001
pyinstaller rec_f.spec
OCR虛擬環(huán)境打包腳本
pipenv --three
pipenv shell
pip install D:\program\python\paddlepaddle-1.7.2-cp37-cp37m-win_amd64.whl
pip install flask
pip install tornado
網(wǎng)絡(luò)好的話可以跳過以下兩步
pip install D:\program\python\opencv_python-4.4.0-cp37-cp37m-win_amd64.whl
pip install D:\program\python\matplotlib-3.3.2-cp37-cp37m-win_amd64.whl
pip install D:\program\python\PyQt5-5.15.1-5.15.1-cp35.cp36.cp37.cp38.cp39-none-win_amd64.whl
pip install matplotlib==3.3.0
AttributeError: module 'sip' has no attribute 'setapi'
pip install --upgrade matplotlib
pip install pyqt5
python -m pip install pypiwin32
pip uninstall matplotlib
pip uninstall pyqt5
WinXP虛擬環(huán)境安裝
pipenv --three
pipenv shell
pip install D:\program\python\paddlepaddle-1.5.2-cp37-cp37m-win_amd64.whl
pip install D:\program\python\paddlepaddle-1.6.3-cp37-cp37m-win_amd64.whl
網(wǎng)絡(luò)好的話可以跳過以下兩步
pip install D:\program\python\opencv_python-4.4.0-cp37-cp37m-win_amd64.whl
pip install D:\program\python\matplotlib-3.3.2-cp37-cp37m-win_amd64.whl
pip install flask
pip install D:\program\python\numpy-1.19.3-cp37-cp37m-win_amd64.whl
ImportError: DLL load failed: 動(dòng)態(tài)鏈接庫(kù)(DLL)初始化例程失敗
- 卸載目前安裝的tensorflow:pip uninstall tensorflow
- 安裝舊版本的tensorflow:pip install --ignore-installed --upgrade tensorflow==1.5
-c的作用是返回錯(cuò)誤信息!如果有報(bào)錯(cuò)的話,將在控制臺(tái)顯示。這里,要做好截圖的準(zhǔn)備,因?yàn)榭刂婆_(tái)報(bào)錯(cuò)后是一閃而過的,如下。
pyinstaller -p C:\Users\PL-RD.virtualenvs\no_server-NBCSumUj\Lib\site-packages -D -c rec.py
pyinstaller -p C:\Users\PL-RD.virtualenvs\no_server-NBCSumUj\Lib\site-packages -F -c rec.py
pyinstaller -p C:\Users\PL-RD.virtualenvs\env-A89Qz4pK\Lib\site-packages -D -c rec.py
pyinstaller -p C:\Users\PL-RD.virtualenvs\env-A89Qz4pK\Lib\site-packages -F -c rec.py
pyinstaller -p C:\Users\PL-RD.virtualenvs\pack_exe-NbvpC7L7\Lib\site-packages -D -c api.py
pyinstaller -p C:\Users\PL-RD.virtualenvs\pack_exe-NbvpC7L7\Lib\site-packages -F -c api.py
pyinstaller api.py --hidden-import pkg_resources
pyinstaller -p C:\Users\PL-RD.virtualenvs\pack_exe-NbvpC7L7\Lib\site-packages -D -c rec.py
pyinstaller -p C:\Users\PL-RD.virtualenvs\pack_exe-NbvpC7L7\Lib\site-packages -F -c rec_f.py
pyinstaller -p C:\Users\PL-RD.virtualenvs\model_api-drqWMESO\Lib\site-packages -F -w rec_f.py
get all libraries used in the script from Lib\site-packages to your application directory will work