7
私が書かれているデータ解析コードのラインプロファイリングを実行した後、私は、総運転時間の約70%がへの呼び出しに集中していることを見出しました2つの異なる配列操作ルーチン。私は最終的にはリアルタイムでデータを分析したいので、ここでの最適化は大幅に役立ちます。
二つの機能は左側にマトリックスを取り、右のフォーム(およびその逆)にそれをもたらします。
私が興味を持っている行列は、現在、N×N個のnumpy配列(Nは偶数)で格納されています。
コード:
私はこれを達成するために、次のコードを書かれている:
# Shifts elements of a vector to the left by the given amount.
def Vec_shift_L(vec, shift=0):
s = vec.size
out = np.zeros(s, dtype=complex)
out[:s-shift] = vec[shift:]
out[s-shift:] = vec[:shift]
return out
# Shifts elements of a vector to the right by the given amount.
def Vec_shift_R(vec,shift=0):
s=vec.size
out=np.zeros(s, dtype=complex)
out[:shift] = vec[s-shift:]
out[shift:] = vec[:s-shift]
return out
# Shifts a matrix from the left form (above) to the right form.
def OP_Shift(Trace):
s = Trace.shape
Out = np.zeros(s, dtype=complex)
for i in np.arange(s[0]):
Out[i,:] = Vec_shift_L(Trace[i,:], (i+s[0]/2) % s[0])
for i in np.arange(s[0]):
Out[i,:] = np.flipud(Out[i,:])
return Out
# Shifts a matrix from the right form (above) to the left form.
def iOP_Shift(Trace):
s = Trace.shape
Out = np.zeros(s, dtype=complex)
for i in np.arange(s[0]):
Out[i,:] = np.flipud(Trace[i,:])
for i in np.arange(s[0]):
Out[i,:] = Vec_shift_R(Out[i,:], (i+s[0]/2) % s[0])
return Out
もともとこれを書いたとき、私はnumpyののロール機能を知らなかったので、私はその中でvec_shift機能を書いていました場所。私の現在のシステムでロールを使用すると、パフォーマンスは〜30%向上しているようです。
さらにこのコードのパフォーマンスを向上させる方法はありますか?
ありがとうございました!私はこのようなエレガントな方法で、互換性のない行列形状の操作を実行できることはわかりませんでした。これはかなり役に立ちます。 :D – DMilden