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.













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