File Exchange Pick of the Week

Our best user submissions

How our developers make MATLAB faster

Last week, we had a Puzzler [click here] that could be developed faster and easier by using the Image Processing Toolbox. Some people used the toolbox, and some did not. As the conversation in the comments continued [click here] we scaled the problem up by solving the problem thousands of times. At this scale, a bottleneck in RMFIELD, which is used by REGIONPROPS, noticeably slowed the Image Processing based code.

It did not take long for Steve Eddins, of Image Processing fame, to notice this conversation. Soon, one of the developers on the team, Inpakala, worked up a solution that speed up the code 70%!

It is too late for this speed optimization to make the 2008b release of MATLAB, but it should make 2009a once all testing is done. This is a case of writing code clean and clear at first and only then doing optimization where the bottlenecks appear. I would like to talk about this solution, and use some MATLAB tools to explore it.

Here is the crux of the problem, in its simplest form:

im = round(rand(100));
for i = 1:1000
    old = regionprops(im,'area');
end


Looking at the Profiler report for this simple code, [Click here for a video on the Profiler]

MATLAB profiler

we can quickly find the slowest part of each m-file until we hit the culprit.

MATLAB profiler

arrow.jpg
MATLAB profiler

arrow.jpg
MATLAB profiler

By looking at the slowest line each time, Inpakala found that the slow line of code was from the use of STRMATCH, which is called by RMFIELD. Since that file is not something she can easily modify, just like any other MATLAB user, she needed to find a different way of writing the code she could control.

Lets look at her modified code using the file comparison tool:

MATLAB file comparison tool

We find that around the trouble line, Inpakala mde the following changes.

MATLAB file comparison tool

The analysis of the actual code changes made is really beyond the scope of this entry, but basically she changed two lines of code (in red) and added three others (in green) along with a new ten line subfunction (not shown).

In the final analysis: the code got longer, more complicated and faster. Notice the code started with the simplest code that would work, and only when optimization was needed was it attempted. In the end, this optimization knocked 70% off of the runtime for REGIONPROPS.

MATLAB profiler

Do you find bottlenecks with the profiler? If you find a bottleneck in MATLAB code that could be improved, don’t be shy! Tell us about it!

|
  • print

コメント

コメントを残すには、ここ をクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。