python使用jit没加速_numba @jit会降低纯Python的速度吗?

您不能期望numba在这种简单的矢量化操作上胜过numpy.另外,由于numba函数包括外部函数调用的开销,因此您的比较也不完全公平.如果对一个更大的数组求和,您将看到两个函数的性能收敛,并且您看到的只是非常快速的操作的开销:

import numpy as np

import numba as nb

@nb.njit

def asd(x,y):

return x+y

def asd2(x, y):

return x + y

u=np.random.random(10000)

w=np.random.random(10000)

%timeit asd(u,w)

%timeit asd2(u,w)

The slowest run took 17796.43 times longer than the fastest. This could mean

that an intermediate result is being cached.

100000 loops, best of 3: 6.06 ?s per loop

The slowest run took 29.94 times longer than the fastest. This could mean that

an intermediate result is being cached.

100000 loops, best of 3: 5.11 ?s per loop

至于并行功能,对于此简单操作,可以使用nb.vectorize:

@nb.vectorize([nb.float64(nb.float64, nb.float64)], target='parallel')

def asd3(x, y):

return x + y

u=np.random.random((100000, 10))

w=np.random.random((100000, 10))

%timeit asd(u,w)

%timeit asd2(u,w)

%timeit asd3(u,w)

但是同样,如果您对小型数组进行操作,将会看到线程分派的开销.对于上面的数组大小,我看到并行使我的速度提高了2倍.

numba真正发挥作用的地方是使用广播难以执行numpy的操作,或者当操作会导致大量临时中间数组分配时.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值