Numpy + Python比MATLAB慢15倍? [英] Numpy + Python 15x slower than MATLAB?

查看:332
本文介绍了Numpy + Python比MATLAB慢15倍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我在Python/Numpy中编写的一些代码,这些代码几乎是我从MATLAB代码直接翻译而来的.当我在机器上的MATLAB中运行代码时,大约需要17秒钟.当我在计算机上以Python/Numpy运行代码时,大约需要233秒.我没有有效地使用Numpy吗?请查看我的Python代码,查看我是否以无效方式使用了Numpy.

Here is some code I wrote in Python / Numpy that I pretty much directly translated from MATLAB code. When I run the code in MATLAB on my machine, it takes roughly 17 seconds. When I run the code in Python / Numpy on my machine, it takes roughly 233 seconds. Am I not using Numpy effectively? Please look over my Python code to see if I'm using Numpy in a non effective manner.

import numpy as np
from numpy import * 
import pylab as py
from pylab import *
import math
import time

def heat(D,u0,q,tdim):
    xdim = np.size(u0)
    Z = np.zeros([xdim,tdim])
    Z[:,0]=u0;
    for i in range(1,tdim):
        for j in range (1,xdim-1):
            Z[j,i]=Z[j,i-1]+ D*q*(Z[j-1,i-1]-2*Z[j,i-1]+Z[j+1,i-1])
    return Z

start_time = time.clock()
L = 10
D = 0.5

s = 0.03  # magnitude of noise

Tmax = 0.2
xdim = 25
tdim = 75 

x = np.linspace(0,L,xdim)
t = np.linspace(0,Tmax,tdim)

dt = t[1]-t[0]
dx = x[1]-x[0]

q = dt/(dx**2)
r1 = 0.75*L
r2 = 0.8*L


################################################
## check the stability criterion dt/(dx^2)<.5 ##
################################################

# Define the actual initial temperature distribution
u0 = np.zeros(xdim)
for i in range(0,xdim):
    if(x[i]>=r1 and x[i]<=r2):
        u0[i] = 1
xDat = range(1,xdim-1)
tDat = np.array([tdim])
nxDat = len(xDat)
ntDat = 1
tfinal = tdim-1

# synthesize data
Z = heat(D,u0,q,tdim)
u = Z[xDat,tfinal] # matrix
uDat = u + s*randn(nxDat)

# MATLAB PLOTTING
#figure(1);surf(x,t,Z); hold on;
#if ntDat>1, mesh(x(xDat),t(tDat),uDat);
#else set(plot3(x(xDat),t(tDat)*ones(1,nxDat),uDat,'r-o'),'LineWidth',3);
#end; hold off; drawnow


#MCMC run
N = 10000
m = 100
XD = 1.0
X = np.zeros(N)
X[0] = XD
Z = heat(XD,u0,q,tdim)
u = Z[xDat,tfinal]
oLLkd = sum(sum(-(u-uDat)**2))/(2*s**2)
LL = np.zeros(N)
LL[0] = oLLkd

# random walk step size
w = 0.1
for n in range (1,N):
    XDp = XD+w*(2*rand(1)-1)
    if XDp > 0:
        Z = heat(XDp,u0,q,tdim)
        u = Z[xDat,tfinal]
        nLLkd = sum(sum( -(u-uDat)**2))/(2*s**2)
        alpha = exp((nLLkd-oLLkd))
        if random() < alpha:     
            XD = XDp
            oLLkd = nLLkd
            CZ = Z
    X[n] = XD;
    LL[n] = oLLkd;

print time.clock() - start_time, "seconds"

推荐答案

Numpy和Matlab在基本数组/矩阵操作上的性能差异很可能是由于Numpy是针对较慢的Lapack实现而安装的.为了获得最佳性能,您可以考虑针对LAPACK构建numpy(此处的说明).

Difference in performance between Numpy and Matlab for basic array/matrix operation is most probably due to Numpy being installed against a slower Lapack implementation. For maximum performance you may consider building numpy against LAPACK (instructions here).

在您的代码中,主要的性能问题是您基本上是在python for循环中执行抽搐.因此,您并没有真正使用Numpy功能.您应该使用专用的卷积函数(例如scipy.ndimage.convolve)替换double for循环.

In your code the main performance hit is that you are basically performing a convultion in a python for-loop. Thus your are not really making use of Numpy capabilities. You should replace your double for loop with a dedicated convolution function such as scipy.ndimage.convolve.

这篇关于Numpy + Python比MATLAB慢15倍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆