File Exchange Pick of the Week

October 19th, 2006

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.

30 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!

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

    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 :

    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.

  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 ?

    Hope to get an answer,
    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 :

    Thanks for fast reply,
    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!

  29. adnyana replied on :

    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

  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.

Leave a Reply

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).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Zach: Hi Doug and Les, I didn’t have a lot of time to mess with this, but I did find a work-around. I plotted...
  • hamed: k
  • Les: @Zach This isn’t exactly what you are looking for but at least it puts all three parameters on the same...
  • Zach: Thanks for your suggestions Doug. I’ll give that a shot and see what happens. I’ve seen many of...
  • Doug: @Zach, I would say to use plotYYY, because that is close to what you want, but using depth as Y makes sense....
  • Doug: @Teja, I think this will work: http://www.mathworks .com/access/helpdesk /help/techdoc/ref...
  • Gify: merry christmas :) nice christmas tree! Regards, Janet Gify
  • Teja: Dear Doug Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i...
  • Zach: I’m working with geophysical data, so I’d like to produce a depth profile. The y-axis would be...
  • Doug: @Ashok First, please do not use variable names that are MATLAB commands (std and mean). Second, p(j) should be...

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