Is MATLAB a compiler?
105 views (last 30 days)
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?
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.
Image Analyst on 24 Sep 2017
MATLAB has a compiler: http://www.mathworks.com/access/helpdesk/help/toolbox/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.