确实没有比这更快的速度,这些是您的选择:
numpy.outer
>>> %timeit np.outer(a,b)100 loops, best of 3: 9.79 ms per loop
numpy.einsum
>>> %timeit np.einsum('i,j->ij', a, b)100 loops, best of 3: 16.6 ms per loop
麻巴
from numba.decorators import autojit@autojitdef outer_numba(a, b): m = a.shape[0] n = b.shape[0] result = np.empty((m, n), dtype=np.float) for i in range(m): for j in range(n): result[i, j] = a[i]*b[j] return result>>> %timeit outer_numba(a,b)100 loops, best of 3: 9.77 ms per loop
Parakeet
from parakeet import jit@jitdef outer_parakeet(a, b): ... same as numba>>> %timeit outer_parakeet(a, b)100 loops, best of 3: 11.6 ms per loop
赛顿
cimport numpy as npimport numpy as npcimport cythonctypedef np.float64_t DTYPE_t@cython.boundscheck(False)@cython.wraparound(False)def outer_cython(np.ndarray[DTYPE_t, ndim=1] a, np.ndarray[DTYPE_t, ndim=1] b): cdef int m = a.shape[0] cdef int n = b.shape[0] cdef np.ndarray[DTYPE_t, ndim=2] result = np.empty((m, n), dtype=np.float64) for i in range(m): for j in range(n): result[i, j] = a[i]*b[j] return result>>> %timeit outer_cython(a, b)100 loops, best of 3: 10.1 ms per loop
茶野
from theano import tensor as Tfrom theano import functionx = T.vector()y = T.vector()outer_theano = function([x, y], T.outer(x, y))>>> %timeit outer_theano(a, b)100 loops, best of 3: 17.4 ms per loop
py
结论:# Same pre as the `outer_numba` function>>> timeit.timeit("outer_pypy(a,b)", number=100, setup="import numpy as np;a = np.random.rand(128,);b = np.random.rand(32000,);from test import outer_pypy;outer_pypy(a,b)")*1000 / 100.016.36 # ms
╔═══════════╦═══════════╦═════════╗║ method ║ time(ms)* ║ version ║╠═══════════╬═══════════╬═════════╣║ numba ║ 9.77 ║ 0.16.0 ║║ np.outer ║ 9.79 ║ 1.9.1 ║║ cython ║ 10.1 ║ 0.21.2 ║║ parakeet ║ 11.6 ║ 0.23.2 ║║ pypy ║ 16.36 ║ 2.4.0 ║║ np.einsum ║ 16.6 ║ 1.9.1 ║║ theano ║ 17.4 ║ 0.6.0 ║╚═══════════╩═══════════╩═════════╝* less time = faster
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)