Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: much faster MATLAB
Date: Thu, 9 Oct 2008 13:53:24 +0000 (UTC)
Organization: Erasmusmc
Lines: 20
Message-ID: <gcl2ck$pl$1@fred.mathworks.com>
References: <gcebi7$otk$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1223560404 821 172.30.248.37 (9 Oct 2008 13:53:24 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 9 Oct 2008 13:53:24 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1095751
Xref: news.mathworks.com comp.soft-sys.matlab:494345


I disagree with the general opinion that it is hard to make faster code with MEX files. Matlab does not handle some cases efficiently. 

For example, when working with large matrices:
A = zeros(n, n);
for j=1:many
  B = sparse(n, n, 0.01);
  A = A + B;
end

So, adding a sparse matrix to a full matrix, many times. Matlab first creates a temporary matrix T = A+B, and then assigns A:=T, deleting the previous A from memory. For large matrices, this reallocating consumes considerable time.

Writing a simple MEX file that overwrites the data in A:
MexAddMatrix(A,B);
bypasses the memory allocation for A.

MEX files can also speedup many elementary numerical work, e.g. raytraycing.

Greetings,
Sebastiaan