Fall in love with python series-python performance (13): avoid using for loop

I have always wondered why the speed of scalar and vector calculation is different, until I read this article

Next experiment:

Use a for loop:

import  numpy as np
ls=[i+8 for i in range(1<<25)]
arrs=np.array(ls)

#定义一个对每个元素都加8的函数
def add_8(nums):
    for i in range(len(nums)):
        nums[i]+=8
    return nums
%timeit add_8(arrs)

结果:9.1 s ± 308 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Without for loop:

import  numpy as np
ls=[i+8 for i in range(1<<25)]
arrs=np.array(ls)
%timeit arrs+8

结果:30.9 ms ± 27.5 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

The gap is too big

Using a for loop is a scalar calculation, and using a general function (addition here) is equivalent to a vector calculation

However, if you want to use a for loop, you must use numba

Let's see the effect

import  numpy as np
from numba import jit
ls=[i+8 for i in range(1<<25)]
arrs=np.array(ls)

#定义一个对每个元素都加8的函数
@jit(nopython=True)
def add_8(nums):
    for i in range(len(nums)):
        nums[i]+=8
    return nums
%timeit add_8(arrs)

结果:17.6 ms ± 57.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

The effect is better than the general function, numba becomes a god

 

Guess you like

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