Take the 2-minute tour ×

I have to prove to my client that Fortran is faster than Matlab/Simulink. He is considering migrating a code from fortran to Matlab. The code is mainly logic and "procedural" subroutines. It does not use any native matrix operations or mathematical functions (eigenvalues, non linear equations, etc) I think that the question of who is faster is already answered considering several references over the internet and the "intrinsic characteristics" of each language, but I need concrete data.

All charts that I found compare Matlab/Simulink x Fortran but do not specify if the Matlab code is compiled or not (using matlab coder toolbox). I think that it is a critical issue.

I´m not saying that compiling the code will make matlab faster than fortran, but in order to really convince someone I would like to see the results.

A good start would be:

  1. Performance - Matlab (.m) compiled (Matlab coder toolbox) X Intel Fortran
  2. Performance - Simulink compiled (Realtime toolbox) X Intel Fortran

Does anyone have already tested this scenario?

share|improve this question
@edited -> Matlab Compiler changed to Matlab coder –  guilhermecgs Oct 8 '13 at 20:31
add comment

3 Answers

up vote 2 down vote accepted

You would need to ask a more tightly defined question - there's no single answer to whether Fortran is faster than MATLAB/Simulink.

First of all, it's easy to write terrible, slow algorithms in either language. So you'd need to specify particular, well-written algorithms.

Secondly, there are many things for which MATLAB will be faster than even very well-written Fortran (or C). For example, if you want to multiply two big matrices together, or calculate some eigenvalues, or other linear algebra that is in MATLAB's sweet spot, you won't beat it. On the other hand if you're doing something with a lot more logic, that can't be vectorised, Fortran is likely to be faster (as long as it's written well).

When you introduce MATLAB Coder into the picture, these latter things are the ones that are most likely to benefit from a speedup by converting to C code (mostly because the former things really can't be sped up much, which is why you wouldn't beat them). But the speedup is variable - I've seen over 10-15x, but also sometimes only 1-2x.

You don't mention where you found the charts you have comparing MATLAB to Fortran, but if you've found them on the internet I would think it's a pretty safe assumption that they don't involve C code generation with MATLAB Coder, and represent the performance of just MATLAB.

Finally - one other method of speeding up MATLAB is to parallelize it with Parallel Computing Toolbox (which enables you to parallelize things over the cores on your local machine) and possibly also with Distributed Computing Server (parallelization on cluster). It's typically a lot easier to do this with MATLAB code than it is to speed up by using MATLAB Coder to produce C code - so if you think it's critical to consider MATLAB Coder in your comparisons, you should probably also consider this as well.

share|improve this answer
Thank you for your answer. 1) I have a code already written in fortran, and my client wants to migrate it to Matlab due to some other reasons. So, there is a high probability that the conde contains mostly logic that can't be vectorised (and I agree that it is a relevant information and should have been mentioned in the question). –  guilhermecgs Oct 9 '13 at 13:20
2) The source I obtained is from ilnumerics.net/blog/… . 3) The parallel Computing Toolbox is indeed a good solution, but since I already have a computing grid running quite well, I will generate a standalone executable that later on will use this grid for running several cases in parallel. It is not parallel by defition, but it will improve the overall speed –  guilhermecgs Oct 9 '13 at 13:25
Depending on the reasons for which your client wishes to migrate to MATLAB from Fortran, and the particular nature of your code, you might find that a useful middle ground would be to migrate the main application to MATLAB (giving a boost to ease of development), but keeping the performance-intensive parts in Fortran - calling them from MATLAB via the MATLAB MEX interface. –  Sam Roberts Oct 9 '13 at 15:40
add comment

Matlab code that I recently "compiled" using the Matlab Coder produced a speed-up of x20 (!). The actual expected speedup depends on many things. If your Matlab code is highly vectorized and uses mainly linear-algebra routines, then the Coder is unlikely to produce much speedup. But if you have multiple loops and conditionals in your algorithm then you can indeed achieve order-of-magnitude speedup as in my example above.

Under the hood, Matlab's linear-algebra uses BLAS/LAPACK (via the MKL/ACML libraries), that use highly-optimized Fortran code. So unless you write extremely efficient Fortran, it is not likely that you will be able to outperform Matlab (despite the function-call overheads) for highly-vectorized Matlab linear-algebra/math algos. However, if your code uses conditionals/loops and similar non-math programming constructs, then the picture might change. In short, there's no simple answer - it depends on your specific algorithm/program.

Putting performance aside for the moment, Matlab has numerous other benefits over Fortran, including a vast array of tested built-in functions and enabling a rapid development cycle.

share|improve this answer
add comment

MATLAB Compiler will not make your code faster, it is intended for distributing your code to third party users that do not have MATLAB. You need to provide, along with your compiled code, the MCR or MATLAB Component Runtime, which is essentially a headless version of MATLAB, and which you can distribute freely if you have a license of MATLAB Compiler.

Now, if you use MATLAB Coder (or Simulink Coder for Simulink) to generate C code from your MATLAB code, then it is likely that you will get a speed up compared to interpreted MATLAB code. Even then, that depends on the code in question. Also, this only supports a subset of the MATLAB language, that is compatible with C code generation.

share|improve this answer
PS: can't comment on the FORTRAN bit, as I've never used it. However, MATLAB uses FORTRAN routines underneath the hood. –  am304 Oct 8 '13 at 20:21
Fortran hasn't been "FORTRAN" in 22 years. Please update your dictionary. –  Kyle Kanos Oct 9 '13 at 1:08
add comment

Your Answer


