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 :

ProfilerOnRibbon

And then, you get this :

Profiler

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 :

ProfilerSummary

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 :

ProfilerCodeView

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.

This entry was posted in Beginners, Optimizing your code. Bookmark the permalink.

8 Responses 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.

  2. Erik says:

    There is a danger to optimising too early. Optimising for speed rarely leads to more readable code (Except vectorisation of course) For ‘good’ code, I would prioritise fixing mlint/checkcode warnings over performance.

    Also get user feedback. The code might not do what they want and will need changing or they may be happy with the user experience. In either case your time spent optimising is potentially wasted.

  3. Bolun Chen says:

    Thanks for the post! Profiler is always a good tool in optimizing the code. In C/C++, it’s nearly impossible to obtain such a detailed report time-spend on each function.

    P.S. No offence but I hope you’ve updated your Mac in 2014. 46 sec was too long even in 2012…

    • Jerome says:

      Yes, actually, I think this profiler is one of the best of any programming language I have seen so far (maybe some reader could comment on this).

      And no, still on my old 4-years old Macbook. It still works great. For heavy calculation, I connect to bigger machines…
      I actually made a bug report on the slowness of the waitbar with Mathworks (with a suggested fix). It was confirmed with the engineers there. I don’t know whether the waitbar is faster in R2014a.

  4. Wenwu says:

    Just wondering, how could you tell one program that was optimized using profiler from the ones that were not. Any obvious character or difference?

    • Jerome says:

      Good questions. Well, for example, I often noticed that folks would run an important piece of code with a lot of updates being done to GUIs or displaying tons of information on the command line.

      With the profiler, you very quickly realize that you should limit any interruption of important calculations to display anything (which is why I chose the waitbar example).

      Some other times, the code is poorly refactored, just as if directly written from some coder mind without esthetic or speed considerations. Running the profiler is a first step toward looking at your code in a complete different way. If you don’t even know it existed, it is a sign you never considered other, more efficient, ways to write your calculation.

  5. Kedar Kulkarni says:

    Hello,
    As per your directions I am not getting ‘TestProfilerCode’ under ‘Functions’
    Please let me know any suggestions
    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *