4

これは、Python で JIT を使用する最初の試みであり、スピードアップしたいユース ケースです。numba について少し読んだところ、十分に単純に思えましたが、次のコードでは速度が向上しませんでした。私が犯しているかもしれない明らかな間違いを許してください。

また、cython の基本的なチュートリアルが示唆することを実行しようとしましたが、時間に違いはありませんでした。 http://docs.cython.org/src/tutorial/cython_tutorial.html

変数を宣言するようなことをしなければならないと思いますか?他のライブラリを使用しますか? すべてに排他的に for ループを使用しますか? 参考になるガイダンスや例をいただければ幸いです。

たとえば、以前の質問から、numpy と比較して mpmath の Elementwise operations が遅いことと、 mpmath の代わりに gmpy を使用した方がはるかに高速であるという解決策を知っています。

import numpy as np
from scipy.special import eval_genlaguerre
from sympy import mpmath as mp
from sympy.mpmath import laguerre as genlag2
import collections

from numba import jit

import time

def len2(x):
    return len(x) if isinstance(x, collections.Sized) else 1

@jit # <-- removing this doesn't change the output time if anything it's slower with this
def laguerre(a, b, x):
    fun = np.vectorize(genlag2)
    return fun(a, b, x)

def f1( a, b, c ):

    t       = time.time()
    M       = np.ones( [ len2(a), len2(b), len2(c) ] )
    A, B, C = np.meshgrid( a, b, c, indexing = 'ij' )
    temp    = laguerre(A, B, C)
    M      *= temp
    print 'part1:      ', time.time() - t
    t       = time.time()

    A, B    = np.meshgrid( a, b, indexing= 'ij' )
    temp    = np.array( [[ mp.fac(x1)/mp.fac(y1) for x1,y1 in zip(x2,y2)] for x2,y2 in zip(A, B)] )
    temp    = np.reshape( temp, [ len(a), len(b), 1 ] )
    temp    = np.repeat(  temp, len(c), axis = 2 )
    print 'part2 so far:', time.time() - t
    M      *= temp
    print 'part2 finally', time.time() - t
    t       = time.time()

a = mp.arange( 30 )
b = mp.arange( 10 )
c = mp.linspace( 0, 100, 100 )

M = f1( a, b, c)
4

1 に答える 1