程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Using Python to speed up Python code execution

編輯:Python
  • python The efficiency of program execution is significantly lower than c Of .
  • Optimize python One idea of the code is : take python Core code ( It usually needs to be called many times , Or the core loop ) convert to c Language .
  • There is no need to rewrite the program to c, Only need to use Cython Bao Jiang python The program can be converted .

1.Cython package

  • Cython For package installation, see https://pypi.org/project/Cython/
    pip install Cython
    conda install Cython
  • General installation anaconda When it comes to Cython package ; You can check to see if you already have this package .( Directly in the program import Cython, If no error is reported, it means that there is ; It can also be done through pip list Etc )

2.python–>c

2.1

  • Rename the program to be converted to xxxx.pyx;
    For example, I have an encapsulated class .py file word_frequency.py, Need to be renamed to word_frequency.pyx

2.2

  • Create a file in the same directory setup.py; effect : Use cython The compiler compiles pyx file
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize("word_frequency.pyx"))

2.3

  • Start compilation , On the command line :
    python setup.py build_ext --inplace
    (# inplace Indicates that the source code is in the same directory )

  • If compilation is successful , Then two files will appear in the directory :word_frequency.c,word_frequency.cp38-win_amd64.pyd( If in Linux Under the platform word_frequency.cp38-win_amd64.so), here .so Documents or .pyd Files can be like ordinary python file , By import.

  • In the above documents word_frequency.c It's no use , You can delete ;word_frequency.cp38-win_amd64.pyd The file can be renamed to word_frequency.pyd.

2.4

  • Call normally in other files word_frequency.pyd that will do ( Can be word_frequency.py Change it to another name , So as not to call wrong ).
from word_frequency import word_frequency # Same import
wordFreq = word_frequency(dict_fullpath) # The same way of using 

2.5

  • contrast
 Before conversion :130.40097641944885 s
After the transformation :68.84645509719849 s
  • The speed has only doubled .
  • Cython Can compare with conventional Python How much faster the code actually depends on the code itself . for example , If you run a multivariable cycle with a high cost ,Cython Obviously superior to conventional Python Code . Recursive functions also make Cython Faster than Python.

3 About .pyd file

  • If you want to be right python File encryption , Will be able to python The module is compiled into .pyd file
  • pyd File is made up of D A language written in dll file , At present, there is no way to decompile , Can only be disassembled . to python Files provide high security . And you can also write some python plug-in unit , convenient python Development .

Reference resources :

  • How to optimize your python Code
  • Windows Next Cython Use
  • windows cython Quick start

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved