Doug's MATLAB Video Tutorials

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

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

Great idea for a first video to share!

How about sharing a little info about how you made the video?

Thanks

Mark

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?

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

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.

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

“I really gained some knowledge from this video to find the time taken by the functions and variables”

Thanks for the videos

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

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

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.

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?

I’am experiencing the same problem as Morten L. Jorgensen.
Is their anyone familiar with this problem, and its solution?

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]

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

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

Add A Comment

What is 5 + 9?

Preview: hide

These postings are the author's and don't necessarily represent the opinions of MathWorks.