From: Rune Allnor <>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Can't MEX my functions
Date: Mon, 25 Jul 2011 22:28:14 -0700 (PDT)
Lines: 61
Message-ID: <>
References: <j0krg0$fcg$>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: 1311658564 26797 (26 Jul 2011 05:36:04 GMT)
NNTP-Posting-Date: Tue, 26 Jul 2011 05:36:04 +0000 (UTC)
Injection-Info:; posting-host=; posting-account=VAp5gAkAAAAmkCze5hvZtMeedpZWNthI
User-Agent: G2/1.0
X-Google-Web-Client: true
X-Google-Header-Order: ARLEUHCNK
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR
 2.0.50727; InfoPath.2; MS-RTC LM 8; .NET CLR 1.1.4322),gzip(gfe)
Xref: comp.soft-sys.matlab:737588

On Jul 26, 12:42 am, "Chuck37 " <> wrote:
> I have a slow function and we just bought a license for the MATLAB compiler with the intention of converting it to MEX for speedup.  So when I try to do it I get a message that -x is no longer an option because there is no advantage due to JIT, whatever that is.  I find it inconceivable that my nested for loops written in MATLAB are as fast as they would be written in compiled C.  What the heck is going on?  
> Talk about a kick in the teeth; no speedup and I need to explain to my boss why I just wasted $5000.  I hope I'm missing something.

You are.

The matlab compiler does *not* convert the code to optimal or even
decent C. It merely wraps the matlab interpreter and some other
essentials into a stand-alone bundle that allows you to distribute
the application you wrote in matlab to people who do not have
access to a matlab lisence themselves.

Apart from the lisence issue, there is no difference between
code run as m or p files directly under matlab, and 'compiled

That is what you have. Which is not MEX.

MEX is about linking code written in other languages like
Fortran, C or C++, to be executed more or less like other
matlab code.

There are several issues with this:

1) You need to write the code to be MEX'ed in the compiled
   language, not in matlab.
2) You need access to a 3rd party compiler to actually compile
   the code
3) You need to integrate the MEX code with your other matlab

Items 1) and 2) require that you are a proficient user of the
compiled language. You need to know your C, Fortran or whatever,
to get any benefits from MEX. There is no Royal Road to fast
code. Matlab is easy to learn for the uninitated. That's all.

Item 3) is the ciller, what run-time performance is concerned.
Matlab has a lot of overhead in any number if issues. A MEX'ed
function can be very efficient compared to m code, if it is
small and self-contained. If the MEX function is only one
component of a larger system, then any gains optained by
MEXing might easily be lost to overhead when matlab is in

So the bottom line is that it is very hard to get fast matlab
code. I routinely obtain speed improvments on the order 10x -
50x when I write C++ instead of matlab. The downside is that
I no longer have access to a lot of the nifty math functions
matlab have.

Ah, yes. You might want to try to get a refund on your matlab