Doug's MATLAB Video Tutorials

April 17th, 2009

Speeding up Doug’s solution with the profiler

Last week, I posed a Puzzler. I solved it the hard way, because I did not know any of the fancy math that the people in the comments section mentioned. I will leave the closed form solution to them and the cited references. The monte carlo solution that I came up with gave a great opportunity for code optimization via the profiler. Here is a six minute video that shows the four iterations my code took. I ended up restructuring so that I did not have clarifying “do nothing” code. I got rid of a clarifying subfunction, and I stopped keeping track of all the iterations, and just stored the final result in an accumulator. These optimizations cut the execution time by a factor of ten.

8 Responses to “Speeding up Doug’s solution with the profiler”

  1. Naor replied on :

    Doug,
    I was somewhat surprised that there was such a significant overhead to calling a one-liner function. Is this a general feature? Can I speed up my code by simply by replacing every instance of pi with 3.14? Or did I misunderstand something in the video?
    Thanks

  2. dhull replied on :

    Naor,

    One of the things to consider is that in this case, the function was called 500000 times for a total of 1.7 seconds or

    >> 1.7/500000

    ans =

    3.4000e-006

    So, the over head is minuscule, tiny, nothing. But that small amount of overhead times half a million? Well, now it is a noticeable delay.

    This is an extreme case. I would not worry about it normally.

    -doug

  3. Yi Cao replied on :

    Doug

    This is weird. The video I saw on your blog home page is different from the one on the comment page. Maybe you wish to find the reason.

    I initially saw the video on the blog home page and found it was not as you described in the blog, hence just left until I saw some comments there. In order to read these comments, I went into the comment page, then found the actual video is there.

    Yi

  4. Daniel Armyr replied on :

    This viedoe raised a user interface question for me:
    If I profile my scripts from within the profiler and not from the commandline, how can I set profiling options, like what clock to use, weather to clock built-ins, and so on? I found no such menu options just now.

    Sincerely
    Daniel Armyr

  5. dhull replied on :

    Yi,

    Weird, I just checked the main blog and the permalink. They were both the proper video. Please send me your system information via e-mail. Try and clear your cache before trying it again though. I would like to clear this up.

    Thanks,
    Doug

  6. dhull replied on :

    @daniel,

    The User Interface that I used does not have access to all the functionality that the command line does. It is easy to use and fits what most people want when they profile.

    Doug

  7. Baskaran replied on :

    Hi Doug,
    very useful video. Can you pls provide some video tutorial for simulink profiler.

  8. dhull replied on :

    Baskaran,

    You would have to look to Seth:

    http://blogs.mathworks.com/seth

    I only do MATLAB.

    Doug

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


MathWorks

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

Doug's picture

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