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

Still not fast enough?Several ways to help you speed up Python running

編輯:Python

WELL ~ ~ ~WELL ~ ~ ~WELL ~ ~ ~

PythonRunning slow has always been criticized,

One aspect has to do with language,

On the other hand it could be a problem with your code.

We cannot solve the language problem,

Therefore, only programming skills can be used to improve the operating efficiency of the program.

Here are a few programming methods to improve operational efficiency.


有什麼python相關報錯解答自己不會的、或者源碼資料/模塊安裝/女裝大佬精通技巧 都可以來這裡:(https://jq.qq.com/?_wv=1027&k=dwzSWSBK)或者+V:python10010問我

首先,We need to measure the time and space complexity of the code,

Otherwise, it is difficult to feel the changes in the length of the code with our naked eyes.

python中的profilerCan help us measure the time and space complexity of the program.

使用時通過-oArgument to pass in an optional output file to keep performance logs.

具體使用方法如下:

python -m cProfile [-o output_file] my_python_file.py

01 A data structure using a hash table

If you encounter a lot of search operations in the program,

And there are no duplicates in the data,You can use seek instead of loop.

舉例如下:

可以改寫為


02 Vectorization replaces loops

盡量使用基於C構建的Python庫,

例如Numpy,Scipy和Pandas,

And use vectorized simultaneous processing instead of writing a loop that processes a single element of an array multiple times in a program,

Loops are probably the easiest place for program optimization to be exploited.

舉例如下:

Multiply arrays directly when squaring each element in the array,而不是兩個for循環.

03 Reduce the number of lines of code

在編程時,Use some as much as possiblepythonbuilt-in functions to reduce lines of code,

The code appears concise and concise,Greatly improve the efficiency of code operation.

舉例如下:


可以改寫為

04 使用多進程

Most computers are multi-process,

then it can be used when performing operationsPython中的multiproccessing.

Multiprocessing enables parallelization in the code.

when you want to instantiate a new process,訪問共享內存時,

Multiprocessing is expensive,

Therefore, if there is a large amount of data processing, you can consider using multiple processes.

對於少量數據,The use of multiprocessing is not recommended.

舉例如下:


可以改寫為

05 使用Cpython

Cythonis a static compiler,The code can be optimized for you.加載cypthonmagicextend and usecython標記使用cython編譯代碼.

Cpython的安裝:

pip install Cython

Cpython的使用:

06 盡量使用csv替代xlsx

在進行數據處理時,

It takes me longer to load the data intoexcel文件或從excel文件保存數據.

相反,I chose to create multiplecsv文件的路徑,

and created a folder to group the files.

舉例如下:

可以改寫為


07 使用Numba

它是一個JIT(即時)編譯器. 通過裝飾器,Numba將帶注釋的Python和NumPy代碼編譯為LLVM .Divide your function into two parts:

1.The function that performs the calculation - 使用@autojit進行裝飾

2.執行IO的功能


08 使用Dask來並行化Pandas DataFrame

Dask很棒!

It helps me to work with numerical functions in dataframes and in parallelnumpy.

I even tried to scale it on the cluster,它就是這麼簡單!


09 使用Pandarallel庫

Pandarallel可以將pandasThe operation is the same as parallelizing multiple processes,

Use only if you have large datasets.

總結

對於提高Python的性能,The first is to write concisely first,高效的代碼.

We have to make sure that the code doesn't perform the same computation over and over in a loop.

Second don't open for every record in the collection/關閉IO連接.

The third is to ensure that new object instances are not created when they are not needed.

Through a lot of programming practice,It is very important for you to master some advanced programming methods.


有什麼python相關報錯解答自己不會的、或者源碼資料/模塊安裝/女裝大佬精通技巧 都可以來這裡:(https://jq.qq.com/?_wv=1027&k=dwzSWSBK) 或者+V:python10010問我

都看到這裡了,給個贊吧 ~ 這對我來說很重要 ~ ~ ~


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