Stuart’s MATLAB Videos

Watch and Learn

Profiler to find code bottlenecks 31

Posted by Doug Hull,

Take a look at this video to see how to use the profiler to find the slow parts of your code. The movie resolution is a bit of a challenge, so please let me know if this video is useful, and what others you would like to see.
ps. Yes, I got a haircut recently. Comments on that are welcome too.

31 CommentsOldest to Newest

Siamak replied on : 1 of 31
I like these simple videos; they actually show me how a pro uses the same tools that I use everyday, it develops my skills I have used profiler before but I really think that using profiler efficiently needs some skills or at least you have to have some sense of code optization, otherwise you will be trapped in a bottle-neck like correct code A good suggestion for these video series would be how to use a source control system in MATLAB, I am anxious to watch something on it
Mark Ray replied on : 2 of 31
Great idea for a first video to share! How about sharing a little info about how you made the video? Thanks Mark
Andreas replied on : 4 of 31
Hi Doug, I am using the profiler a lot, it's very helpful. But sometimes it returns strange results. Occasionally it tells me that most time (such as >60%) is spent at an "END" statement. I have not yet been able to find information anywhere that explains why the code would spend so much time at a statement that does essentially nothing. Any ideas on that?
Urs (us) Schwarz replied on : 5 of 31
SNIP doug not only having lost a shock of hair... firstly, the loop uses a var BITS, which is NOT pre-allocated (as properly shown in the alternative[?] section) - hence, the demo may broadcast a very false message... customers beware... secondly, the movie can be done (so much!) better... just a pedestrian note with a :-) us
Doug replied on : 7 of 31
BITS indeed is not pre-allocated in the first block of code. That is part of the problem with the original code. These errors in the code are intentional, that is the point of the demonstration. Even with pre-allocation, the new code is a great improvement. In a longer demo, this pre-allocation error is found with the m-lint feature of the profiler. Glad to see people are looking closely.
Doug replied on : 8 of 31
Andreas, I did not know the answer, so I went to development and got this answer: "What the customer is seeing is not a bug but a side effect of how the JIT [Just In Time accelerator] operates. Often at the end of loops it must transfer control back to the interpreter which is an expensive operation. The "end" statements are usually where this time ends up getting assigned when profiling programs like this." Now we all know! Doug
srikanth kadiyala replied on : 10 of 31
"I really gained some knowledge from this video to find the time taken by the functions and variables" Thanks for the videos
Nati replied on : 11 of 31
Hi Doug ! I'm using the profiler a lot. I want to know if there is a way to profile a mex file.The profiler treats it like a built in function. Thank's a lot. Nati
Doug replied on : 12 of 31
Nati, If you want visibility into the C code in a mex file, you will need to use the debuging capabilities of your external development environment. Doug
vincent replied on : 17 of 31
your vedio was very useful i was also thinking that if you can give more examples of how to use matlab will help the world.
Morten L. Jørgensen replied on : 18 of 31
My profiler returns something like 9246494268.503 seconds of total time and self time for some functions, this is odd since i only ran the simulation for 8 seconds :-). What am I doing wrong?
Thijs replied on : 20 of 31
I'am experiencing the same problem as Morten L. Jorgensen. Is their anyone familiar with this problem, and its solution?
Thijs replied on : 21 of 31
The following bug (AMD dual core) was the cause: A workaround is also described. This is bug solved in R2007a.
Michael Israel replied on : 23 of 31
Hi, Great video, actually I have a problem. The profiler shows some "if" statements that take a lot of time, (simple ifs like if a == 1), other ifs and ends take 6 seconds. About end statements, I understand from previous guys but why if statements are so slow, I just want to know if it is some kind of a bug or it is a MATLAB limitation ? Hope to get an answer, Thanks [comment reformatted for clarity- Doug]
Doug replied on : 24 of 31
Michael, I just tested this in the manner you described it. I saw no slowdown on the if statements. Please send the code into tech support and we will see what they can do for you. Thanks, Doug
adnyana replied on : 29 of 31
Mr. Doug, i've run my code on profiler, but nothing i can do after i find line code where the most time spent. that line code is 'ismember'. because i don,t know what the other function work like that but faster. i use that function to check if data from one set is member of other set. could you help me Mr best regard adnyana
Billigflug Bali replied on : 30 of 31
Oh yes. Very useful vid! Thank you. And thanks for telling us how you made the video, I was searching for such a tool, too.