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

Its really fragrant. The 24 operations that make Python code take off

編輯:Python

Hello everyone , We know Python There are many acceleration techniques . Today's article , I've summed it up for you 24 individual , Check for defects and make up for omissions , Learn a new trick every day . Longer content , Recommended collection 、 Focus on .

part Python The skills come from the sharing of small partners in the fan group , Thank you . Want to join the little partner , At the end of the article, the adding method .

One 、 Analyze code run time

The first 1 type : Measure the running time of the code

Trivial method

Quick way (jupyter Environmental Science )

The first 2 type : Measure the average time of code running multiple times

Trivial method

Quick way (jupyter Environmental Science )

The first 3 type : Analyze code run time by calling function

Trivial method


Quick way (jupyter Environmental Science )

The first 4 type : Analyze code run time by line

Trivial method



Quick way (jupyter Environmental Science )

Two 、 Speed up your search

The first 5 type : use set Instead of list Search for

Low speed method



High speed method

The first 6 type : use dict Not two list Matching search

Low speed method

High speed method

3、 ... and 、 Speed up your cycle

The first 7 type : priority of use for Cycle, not while loop
Low speed method

High speed method

The first 8 type : Avoid double counting in the loop body

Low speed method

High speed method

Four 、 Speed up your function

The first 9 type : Replace recursive functions with looping mechanisms

Low speed method


High speed method

The first 10 type : Use recursive caching mechanism to speed up functions

Low speed method

High speed method

The first 11 type : use numba Speed up Python function

Low speed method


High speed method

5、 ... and 、 Use standard library functions for acceleration

The first 12 type : Use collections.Counter Speed up the count

Low speed method

High speed method

The first 13 type : Use collections.ChainMap Speed up dictionary merging

Low speed method


High speed method

6、 ... and , Use numpy Vectorization is accelerated

The first 14 type : Use np.array Instead of list

Low speed method

High speed method

The first 15 type : Use np.ufunc Instead of math.func

Low speed method

High speed method

The first 16 type : Use np.where Instead of if

Low speed method


High speed method

7、 ... and 、 Speed up your Pandas

The first 17 type : Use np.ufunc Function instead of applymap

Low speed method

High speed method

The first 18 type : Use pre allocated storage instead of dynamic capacity expansion

Low speed method

High speed method

The first 19 type : Use csv**** File reading and writing instead of excel File read and write

Low speed method

High speed method

The first 20 type : Use pandas Multi process tools pandarallel

Low speed method


High speed method

8、 ... and 、 Use Dask Accelerate

The first 21 type : Use dask Speed up dataframe

Low speed method

High speed method

The first 22 type : Use dask.delayed Accelerate

Low speed method


High speed method

Nine 、 Apply multithreading and multiprocess acceleration

The first 23 type : Apply multithreading acceleration IO Intensive task

Low speed method

High speed method

The first 24 type : Apply multi process acceleration CPU Intensive task

Low speed method

High speed method

Recommended articles

  • Addicted to , Recently, a large visual screen has been rolled out for the company ( Source code attached )
  • So elegant ,4 paragraph Python Automatic data analysis artifact is really fragrant
  • Combing for more than half a month , Well prepared 17 A map of knowledge and thinking , Let's talk about statistics this time
  • Year end summary :20 Visual large screen template , Direct application is really fragrant ( The source code is attached at the end of the article )

Technical communication

Welcome to reprint 、 Collection 、 Gain some praise and support !

At present, a technical exchange group has been opened , Group friends have exceeded 2000 people , The best way to add notes is : source + Interest direction , Easy to find like-minded friends

  • The way ①、 Send the following picture to wechat , Long press recognition , The background to reply : Add group ;
  • The way ②、 Add microsignals :dkl88191, remarks : come from CSDN
  • The way ③、 WeChat search official account :Python Learning and data mining , The background to reply : Add group


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