# Doug's MATLAB Video Tutorials

## Profiler to find code bottlenecks

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 Responses to “Profiler to find code bottlenecks”

1. Siamak replied on :

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

2. Mark Ray replied on :

Great idea for a first video to share!

Thanks

Mark

3. Doug replied on :

Mark,

I used Camtasia for the recording.

Doug

4. Andreas replied on :

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?

5. Urs (us) Schwarz replied on :

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

6. Ben replied on :

That is awesome! I never knew this existed! Its cooler then those Progress Bars!

7. Doug replied on :

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.

8. Doug replied on :

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

9. Gopakumar replied on :

Thanks for the videos Doug. Appreciate it :)

10. srikanth kadiyala replied on :

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

Thanks for the videos

11. Nati replied on :

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

12. Doug replied on :

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

13. Luxus replied on :

Great and excellent article t’s realy helpful. Thanks again.

14. Ramjee Prasad Gupta replied on :

Thanks for video

15. torben replied on :

Excellent article it’s realy helpful, keep up the good work!

16. gutschein replied on :

Great articles. Very interesting.

17. vincent replied on :

i was also thinking that if you can give more examples of how to use matlab will help the world.

18. Morten L. Jørgensen replied on :

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?

19. Robert replied on :

A great and useful viedeo.
A big thank!

20. Thijs replied on :

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

21. Thijs replied on :

The following bug (AMD dual core) was the cause:

http://www.mathworks.com/support/bugreports/details.html?rp=310027

A workaround is also described.
This is bug solved in R2007a.

22. Family replied on :

A great and useful viedeo.
A big thank!

23. Michael Israel replied on :

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 ?

Thanks

[comment reformatted for clarity- Doug]

24. Doug replied on :

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

25. Michael Israel replied on :

How can I do it via email?

[Edited for clarity- Doug]

26. Doug replied on :

Contact support at:

http://www.mathworks.com/support/

support@mathworks.com

27. asim replied on :

Thank you very much..I have gone through most of your videos and all are extrmely helpful.

28. Daniel replied on :

great haircut, great picture 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

30. Billigflug Bali replied on :

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.

31. peter replied on :

nice haircut!

 Name (required) E-mail (required, will not be published) Website (optional) Spam protection (required): What is 8 + 10 ?

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>


If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

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