The secret rule for good Matlab code

I came across countless matlab codes from many different programmers and I noticed there is one crucial difference between a good matlab programmer and a bad one : their usage of the Profiler.

When you think you are finished with your code, always, without exception, endlessly, on and on RUN the Profiler on your code.  This is your most important Matlab friend. The one that will  never fail you.  Sometimes, I don’t respect this rule and 90% of the time, my code comes back to me as not being as good as I would like it to be. If everybody would respect this one single rule, the Matlab world would be a better place.

You might not have used it (SHAME on you if you have been using Matlab for a long time) so I will show here how to get access to it.

It is easy. It is here :

And then, you get this :

Simple enough, there is a ‘Start Profiling’ button. But first you need to have some code available. We will take this one from TestProfilerCode.m :

A=rand(100,100);

h=waitbar(0,'Waiting...');
EndOfLoop=2000;

for i=1:EndOfLoop
A=A.^2;
waitbar(i/EndOfLoop,h);
end
delete(h);

So you start the profiler, start your code, wait for the end of the code and then press the ‘Stop Profiling’ button when your calculation is finished.
You will get this :

Now you have a list of functions that have been executed between ‘Start’ and ‘Stop’. You will find the one that we executed ‘TestProfilerCode’.

Now if you click on it. Oh, Magic, you will get this :

This gives you how much time is spent per line! You can even click on each individual function (here the waitbar) to get more details. Here the heavy usage of waitbar clearly pops out.

This is a dream come true to all of us Matlab Optimizers.

Now what you want is that lines that are colored in red should be where you expect them to be. By running the profiler routinely, you will be surprised that it is not always where you think that Matlab gets slow. And with time you learn a lot about how to make good Matlab code.

Related Posts

  • Waiting for the waitbarWaiting for the waitbar
    I love matlab waitbar. I use it everywhere in my matlab code. I always have the feeling my code i...
  • Code is poetryCode is poetry
    Today, I want to give you some insight on how to make clean Matlab code that everyone can under...
  • Look for the loopLook for the loop
    A very well known piece of knowledge is that Matlab is bad at for loop. But you might ask: why? ...
  • The art of vectorizing – Part 2The art of vectorizing – Part 2
    In a previous post, I introduced you how to vectorize your code. Here I show how to use repmat to...
  • Some places, some rulesSome places, some rules
    In my last post, I talked about using In place computation to limit memory usage of your calculat...
This entry was posted in Beginners, Optimizing your code. Bookmark the permalink.

One Response to The secret rule for good Matlab code

  1. Chandresh Sharma says:

    Thanks for such kind of helpful tip.It will be good to make programs much faster.

Leave a Reply