In essence, writing a Matlab program is aligning a succession of command that you send to the Matlab interpreter. Indeed, Matlab provides you with a large library of functions that you can use. The job of Mathworks is to create these functions that do the job in :
- A very efficient way.
- A very general way that is applicable to everyone.
To create all these functions, Mathworks can either create M-file, exactly as you do, and fill in a succession of commands that do the work in both a general and efficient way, or they also can directly write their code in C and precompiled for you very efficient set of libraries that work past Matlab interpreter. This last set of functions are called Built-ins.
You can easily find out if a function is built-in or not. For instance, just type in :
This should give you 5 which means ‘built-in’ as explained on the help.
Now if you ask :
You will get a 2 which is for M-file only functions. The advantage of built-in is that you have direct access to the compiled code so you can expect your command to run at C-like speed. The disadvantage is that you don’t have access to the underlying code. In other words, the command :
will kick you out with “ ’plot’ is a built-in function. “.
The advantage of M-file function is that you have access to the code. Indeed :
gives you the actual command that are executed (as well as the help section).
You might ask, why am I telling you all this?
Because, in many cases, making general codes is not the best way to make very efficient code.
You want only a very precise thing to be done very fast. And sometimes, M-files function can give you problems because they are interpreted.
Luckily, by definition, you still have access to the source code, so you can optimize this. The process of fishing in the original function the only lines of codes that are relevant to you is called Inlining and it is my duty today to explain it to you.
I am going to take a very simple example for you. Don’t underestimate all this, just because the example is too simple. In many cases, you can get a 10 times boost in your code.
So please run this code while your profiler is on.
A=rand(1000,1); for i=1:10000 b=mean(A); end
On my machine, Matlab spend about 6.2 seconds on line 3. This is to be expected. There are a lot of calculations. But now, let’s have a look at the code in mean.
To get inside Matlab functions, you have several ways. Either you locate the file using “which(mean)” or you type in “edit(‘mean.m’) or you do “type(‘mean’)”
In any cases, you will easily realize that you can replace this call to mean with the following code :
A=rand(1000,1); for i=1:10000 b=sum(A,1)/size(A,1); end
Now the profiler on this will tell you that only 0.8 seconds is spent on line 3. This is about 8 times faster! I told you this was a powerful technique…
Still, don’t do this unless it is necessary. Inlining often come at the cost of clarity. In case you want a very practical example of this technique take a look at how I optimized Matlab TIff loader.