python比matlab慢,在这个简单的例子中,为什么Matlab看起来比Python慢​​得多

我最近问了

a question on the optimization of a mask function in Matlab.我得到了两个对我有帮助的答案,但似乎根据我的时间,所有Matlab解决方案看起来都比Numpy解决方案慢得多.不同功能的代码可以在我之前的问题中找到,但为了了解我在做什么,我给出了Numpy“循环”解决方案,当然这不是最快的解决方案,但可能是最简单的解读:

def dealiasing2d(where, data):

nk, n0, n1 = data.shape

for i0 in xrange(n0):

for i1 in xrange(n1):

if where[i0, i1]:

data[:, i0, i1] = 0.

我获得(使用Matlab R2014b和与Blas和Lapack相关联的“基本”Numpy 1.9.1)(n0 = n1 = N):

N = 500 ; nk = 500:

Method | time (s) | normalized

----------------|----------|------------

Numpy | 0.05 | 1.0

Numpy loop | 0.05 | 1.0

Matlab find | 0.74 | 14.8

Matlab bsxfun2 | 0.76 | 15.2

Matlab bsxfun | 0.78 | 15.6

Matlab loop | 0.78 | 15.6

Matlab repmat | 0.89 | 17.8

N = 500 ; nk = 100:

Method | time (s) | normalized

----------------|----------|------------

Numpy | 0.01 | 1.0

Numpy loop | 0.03 | 3.0

Matlab find | 0.15 | 13.6

Matlab bsxfun2 | 0.15 | 13.6

Matlab bsxfun | 0.16 | 14.5

Matlab loop | 0.16 | 14.5

Matlab repmat | 0.18 | 16.4

N = 2000 ; nk = 10:

Method | time (s) | normalized

----------------|----------|------------

Numpy | 0.02 | 1.0

Matlab find | 0.23 | 13.8

Matlab bsxfun2 | 0.23 | 13.8

Matlab bsxfun | 0.26 | 15.6

Matlab repmat | 0.28 | 16.8

Matlab loop | 0.34 | 20.4

Numpy loop | 0.42 | 25.1

在我看来,这些结果非常奇怪.对我来说,Numpy和Matlab在科学计算方面非常相似所以性能应该相似,而这里有10倍以上!所以我的第一个猜测是我比较两种语言的方式有问题.另一种可能是我的Matlab设置有问题,但我不明白为什么.或者Matlab和Numpy之间真正的深刻区别?

有人可以用这些函数来验证这些结果吗?你知道为什么Matlab在这个简单的例子中看起来比Python慢​​得多吗?

为了计算Matlab函数的时间,我使用了一个文件:

N = 500;

n0 = N;

n1 = N;

nk = 500;

disp(['N = ', num2str(N), ' ; nk = ', num2str(nk)])

where = false([n1, n0]);

where(1:100, 1:100) = 1;

data = (5.+1i)*ones([n1, n0, nk]);

disp('time dealiasing2d_loops:')

time = timeit(@() dealiasing2d_loops(where, data));

disp([' ', num2str(time), ' s'])

disp('time dealiasing2d_find:')

time = timeit(@() dealiasing2d_find(where, data));

disp([' ', num2str(time), ' s'])

disp('time dealiasing2d_bsxfun:')

time = timeit(@() dealiasing2d_bsxfun(where, data));

disp([' ', num2str(time), ' s'])

disp('time dealiasing2d_bsxfun2:')

time = timeit(@() dealiasing2d_bsxfun2(where, data));

disp([' ', num2str(time), ' s'])

disp('time dealiasing2d_repmat:')

time = timeit(@() dealiasing2d_repmat(where, data));

disp([' ', num2str(time), ' s'])

我测量Python函数的性能

from __future__ import print_function

import numpy as np

from timeit import timeit, repeat

import_lines = {

'numpy_bad': 'from dealiasing_numpy_bad import dealiasing2d as dealiasing',

'numpy': 'from dealiasing_numpy import dealiasing'}

tools = import_lines.keys()

time_approx_one_bench = 5.

setup = """

import numpy as np

N = 500

n0, n1 = N, N

nk = 500

where = np.zeros((n0, n1), dtype=np.uint8)

where[0:100, 0:100] = 1

data = (5.+1j)*np.ones((nk, n0, n1), dtype=np.complex128)

"""

exec(setup)

print('n0 = n1 = {}, nk = {}'.format(N, nk))

print(13*' ' + 'min mean')

durations = np.empty([len(tools)])

for it, tool in enumerate(tools):

setup_tool = import_lines[tool] + setup

duration = timeit(setup_tool + 'dealiasing(where, data)', number=1)

nb_repeat = int(round((time_approx_one_bench - duration)/duration))

nb_repeat = max(1, nb_repeat)

ds = np.array(repeat('dealiasing(where, data)',

setup=setup_tool, number=1, repeat=nb_repeat))

duration = ds.min()

print('{:11s} {:8.2e} s ; {:8.2e} s'.format(

tool.capitalize() + ':', duration, ds.mean()))

durations[it] = duration

fastest = tools[durations.argmin()].capitalize()

durations = durations / durations.min()

print('Durations normalized by the fastest method (' + fastest + '):')

for it, tool in enumerate(tools):

print('{:11s} {:8.3f}'.format(tool.capitalize() + ':', durations[it]))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值