Is MATLAB a compiler?

105 views (last 30 days)
Snoopy on 24 Sep 2017
Edited: Michael Listovski on 23 Aug 2020
I have been using MATLAB for some time but I am still not clear how MATLAB compares to some alternative software in the following respect. One of these software is Fortran. Some people claim that their optimisation algorithm works (much) faster in Fortran than in MATLAB or in R. When I ask why, the answer I get is that MATLAB is a compiler. What does this mean? What is that MATLAB is compiling and Fortran is not? And if it is indeed true that MATLAB is slower in some optimisation algorithm, compared to Fortran, what exactly is causing this?
Snoopy on 23 Aug 2020
Thanks a lot for this elaborate answer. It is a very contributing explanation.

Sign in to comment.

Answers (3)

Jan on 24 Sep 2017
Edited: Jan on 24 Sep 2017
No, Matlab is not a "compiler", but an "interpreter". A compiler converts the source code to an executable file, which is not readable by human anymore. When working with an interpreter, the readable source code remains the base of what is executed. But even in Matlab the code is interpreted and optimized, here by the "JIT accelerator". This removes e.g. redundant code and re-orders the commands, if it improves the processing speed. But the accelerated code is not written a the disk, but stored in the RAM only. It is debatable, if this can be called "compiling" or not.
In opposite to Matlab, FORTRAN compiles the source code into an executable file. This takes some time, but it is done once only, while Matlab interprets the source code each time it is loaded the first time in a Matlab session (or after a user called the bad clear all).
In fact some code is processed faster in FORTRAN or C compared to MATLAB. MATLAB's JIT accelerator could work more efficiently, I assume. But as usual I point out, that MATLAB as other programming languages is not designed for benchmarks only, but to solve problems. The time to solve a problem consists of different parts:
total time = design + programming + testing and debugging +
documentation + run time
When I create a tiny function for a linear algebra problem:
x = B \ (A * b + c) % A, B: Matrices, b, c: vectors
this can be done very compact in MATLAB. Internally very fast C/Assembler routines of the BLAS libraries are called to perform the calculations. You can call these functions from C or FORTRAN also, but the calling is much more complicated and prone to bugs. Therefore I assume, that programming and debugging will take more time in FORTRAN and especially in C than in MATLAB.
Cedric Wannaz
Cedric Wannaz on 24 Sep 2017
Isn't that funny that we both thought about \ ;-)

Sign in to comment.

Image Analyst
Image Analyst on 24 Sep 2017
MATLAB has a compiler: but it is an extra toolbox that you have to buy. It compiles your source code into a standalone program that others can run without having to buy MATLAB. As I understand it (and use it), it does not speed up the programs over running source code in the development environment.

Snoopy on 24 Sep 2017
From the answers above, I understand that a compiler and interpreter are two very different things, and therefore it does not make (much) sense to ask if MATLAB is a compiler or interpreter. I will now summarise what I understand an interpreter is. Please warn me for my possible misinterpretations, and feel free to edit my message so that we do not take another round to come to a conclusion (if you are able to edit my message). MATLAB is an interpreter and interpretation is about deciding on how to carry out a calculation. This said, MATLAB could be slower according to some benchmark but this is most likely because it allocates some time to select the most robust calculation method before carrying out an calculation. The definition of a compiler is given above. But I am still not so clear whether or how it relates to interpreting a code.
Star Strider
Star Strider on 25 Sep 2017
@ Walter —
If I could give a separate vote to your Comment here, I would.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!