Fall in love with the python series-python performance (11): numexpr is faster than numpy

In the field of python data processing, numpy is used more and more, because numpy is relatively fast, but numexpr will be faster when the amount of data is large

For example, try an experiment first:

First prepare the data:

import numexpr
import numpy as np

ls1=np.array([int(np.random.rand()*100) for i in range(1<<25)])
ls2=np.array([int(np.random.rand()*100) for i in range(1<<25)])

Next, compare the addition:

Use numpy: 

%timeit ls1+ls2

##结果
## 10 loops, best of 3: 74 ms per loop

Use numexpr:

%timeit numexpr.evaluate('ls1+ls2')

##结果
## 10 loops, best of 3: 40.5 ms per loop

The speed of addition has increased, but it is not too big (the larger the amount of data, the more obvious the gap), then look at the multiplication, the change is very big:

Use numpy: 

%timeit ls1**2+ls2**3

##结果
## 1 loop, best of 3: 251 ms per loop

Use numexpr:

%timeit numexpr.evaluate('ls1**2+ls2**3')

##结果
## 10 loops, best of 3: 51.7 ms per loop

This experiment is directly 5 times faster, and in many other experiments it can be dozens of times faster 

numexpr not only speeds up, but also saves memory than numpy, and produces fewer temporary intermediate variables than numpy.

 

 

Guess you like

Origin blog.csdn.net/zhou_438/article/details/109315502