Preface
Python Although the amount of code written is much less than that of C++,Java, But not as fast as they , So there are all kinds of improvements Python Methods and techniques of speed , This time I want to introduce Numba The library accelerates timeconsuming loop operations and Numpy operation .
stay 24 Speed up your Python The acceleration method of the cycle is introduced in , One way is to use Numba
Speed up , Just recently I saw an article introducing the use of Numba
Speed up Python , This paper mainly introduces two examples , It's also Numba
Two functions of , They are accelerated cycle , And right Numpy
The calculation acceleration of .
original text ：https://towardsdatascience.com/hereshowyoucangetsomefreespeedonyourpythoncodewithnumba89fdc8249ef3
Compared with other languages ,Python It's really slow in running speed .
A common solution , Is used as C++ Rewrite the code , And then use Python encapsulate , In this way, we can achieve C++ The operating speed can be maintained in the main applications Python The convenient .
The only difficulty with this approach is to rewrite it as C++ Part of the code takes a lot of time , Especially if you're right about C++ Unfamiliar situations .
Numba
It can improve the speed without rewriting part of the code into other programming languages .
Numba brief introduction
Numba
It is one that can transform Python A compilation library for converting code into optimized machine code . Through this transformation , The running speed of the numerical algorithm can be improved to close to C
The speed of language code .
use Numba
You don't need to add very complex code , Just before the function you want to optimize Add a line of code , Leave the rest to Numba
that will do .
Numba
Can pass pip
install ：
Numba
For those with many numerical operations ,Numpy
Operation or a large number of cyclic operations , Can greatly improve the running speed .
Speed up Python loop
Numba
The most basic application of is acceleration Python Loop operation in .
First , If you want to use loop operation , You first consider whether you can adopt Numpy
Function substitution in , There are some cases , There may be no alternative function . At this time, you can consider using Numba
了 .
The first example is illustrated by inserting a sorting algorithm . We'll implement a function , Enter an unordered list , Then return to the sorted list .
Our husband is a man who contains 100,000 A list of random integers , And then execute 50 Second insertion sorting algorithm , Then calculate the average speed .
The code is as follows ：
Output results ：
From the code, we can know that the time complexity of the insertion sorting algorithm is , Because there are two loops ,for
The loop has while
loop , This is the worst case . Then enter the quantity as 10 All integers , Plus repetition 50 Time , It's a very timeconsuming operation .
The original author used a computer configuration i78700k, So the average time it takes is 3.0104s
. But my computer configuration here is much worse ,i54210M 's Laptop , And has used close to 4 year , So the result of my running is , The average time taken is 22.84s
.
that , How to adopt Numba
Accelerated cycle operation , The code is as follows ：
Output results ：
You can see , In fact, only two lines of code are added , The first line is import jit
Decorator
Then add a line of code in front of the function , Use a decorator
Use jit
Decorator indicates that we want to convert this function to machine code , And then the parameters nopython
Specify that we want Numba
Use pure machine code , Or add some if necessary Python
Code , This parameter must be set to True
To get better performance , Unless something goes wrong .
The average time taken by the original author is 0,1424s
, My computer is upgraded to just 0.094s
, The speed has been greatly improved .
Speed up Numpy operation
Numba
Another common place in , Is to accelerate Numpy
Arithmetic .
This time will initialize 3 It's a very big one Numpy
Array , Equivalent to the size of a picture , Then use numpy.square()
Function squares their sum .
The code is as follows ：
Output results ：
When we are right Numpy
Array for basic array calculation , Such as addition 、 Multiplication and square ,Numpy
Will be automatically vectorized internally , That's why it can be better than native Python
The reason why code has better performance .
The speed of the above code running on the original author's computer is 0.002288s
, And my computer needs 0.04s
about .
But even if it is Numpy
Code won't be as fast as optimized machine code , So... Can still be used here Numba
Accelerate , The code is as follows ：
Output results ：
What we use here is vectorize
Decorator , It has two number parameters , The first parameter specifies the operation to be performed numpy
Data type of array , This must be added , because numba
You need to convert the code to the best version of machine code , In order to increase the speed ;
The second parameter is target
, It has the following three optional values , Indicates how to run the function ：

cpu： Run on a single threaded CPU On

parallel： Run on multicore 、 Multithreading CPU

cuda： Running on the GPU On
parallel
In most cases, the options are faster than cpu
, and cuda
It is generally used when there are very large arrays .
The running time of the above code on the original author's computer is 0.001196s
, Promoted 2 About times , And my computer is 0.0077s
, Promoted 5 About times the speed .
Summary
numba
It can better play its role in increasing speed under the following circumstances ：

Python
The code runs slower thanC
Where to code , Typically, it's a loop operation 
Repeat the same operation in the same place , For example, perform the same operation on many elements , namely
numpy
Array operation
And in other cases ,Numba
It won't bring such a significant speed increase , Of course , In general, try to use numba
Improving speed is also a good attempt .
Last , Exercise code ：
https://github.com/ccc013/Python_Notes/blob/master/Python_tips/numba_example.ipynb
Welcome to my WeChat official account.  The growth of algorithmic apes , Or scan the QR code below , Let's talk , Learning and progress ！