Writing Fast Matlab Code #6:Inlining Simple Functions

November 2, 2009 by Pascal Getreuer 
Filed under: Code Optimization, Tutorials 
Leave a Comment
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
VN:F [1.8.8_1072]
Rating: 9.0/10 (1 vote cast)

Writing Fast Matlab code

Every  time  an M-file function  is called,  the  Matlab interpreter incurs  some overhead.   Additionally, many M-file functions  begin with conditional code that checks the input  arguments for errors or deter- mines the mode of operation.

Of course, this  overhead  is negligible for a single function  call.  It should only be considered  when the function  being called is an M-file, the  function  itself is “simple,”  that is, implemented with only a few lines, and called frequently  from within  a loop.

For example,  this code calls the M-file function  median repeatedly to perform median  filtering:

%   Apply the  median filter  of size 5  to  signal  x y  =  zeros(size(x));   %   Preallocate
 
for k  =  3:length(x)2
 
y(k) =  median(x(k−2:k+2));
 
end

Given a 2500-sample array  for x, the overall run time is 0.42 seconds.

“Inlining  a function”  means to replace a call to the function  with the function  code itself.  Beware that inlining should not  be confused with  Matlab’s “inline”  function  datatype. Studying  median.m (type “edit median” on the console) reveals that most of the work is done using the built-in  sort function. The median call can be inlined as

%   Apply the  median filter  of size 5  to  signal  x y  =  zeros(size(x));   %   Preallocate
 
for k  =  3:length(x)2
 
tmp   =  sort(x(k−2:k+2));
 
y(k) =  tmp(3);             %   y(k)  =  median(x(k−2:k+2));
 
end

Now the  overall  run  time  is 0.047 seconds, nearly  9 times  faster.   Furthermore, by inlining  median, it can be specifically tailored  to evaluating 5-sample  medians.  But  this  is only an example;  if the  Signal Processing  Toolbox➋  is available,  y = medfilt1(x,5) is much faster.

A surprising  number  of Matlab’s functions  are implemented as M-files, of which many  can be inlined in just  a few lines.  In Matlab 5.3 (R11),  the following functions  are worth  inlining:

conv,  cross,  fft2,  fliplr,  flipud,  ifft,  ifft2,  ifftn,  ind2sub,  ismember, linspace,
logspace, mean, median,  meshgrid,  poly, polyval,  repmat,  roots,  rot90,  setdiff,  setxor,  
sortrows,  std, sub2ind, union, unique, var

The list above is for Matlab R11; some of these  functions  have since become built-in.   Use the which

command  or try  “edit function  name”  to determine whether  a function  is implemented as an M-file.

For example,  in Matlab R11 and earlier, ifft was implemented by simply calling fft and conj. If x is a one-dimensional  array,  y = ifft(x) can be inlined with y = conj(fft(conj(x)))/length(x).

Another  example:  b = unique(a) can be inlined with

b  =  sort(a(:));          b( b((1:end1)')  ==  b((2:end)')  ) =  [];

While  repmat has the  generality  to operate  on matrices,  it is often  only necessary  to tile a vector  or just  a scalar.  To repeat  a column vector  y over the columns n times,

A  =  y(:,ones(1,n));                          %   Equivalent to A  =  repmat(y,1,n);

To repeat  a row vector  x over the rows m times,

A  =  x(ones(1,m),:);                                %   Equivalent to A  =  repmat(x,m,1);

To repeat  a scalar s into an m×n matrix,

A  =  s(ones(m,n));                                  %   Equivalent to A  =  repmat(s,m,n);

This  method  avoids the  overhead  of calling an M-file function.   It is never slower than  repmat (critics should  note  that repmat.m itself  uses  this  method  to  construct mind and  nind).  For  constructing matrices  with constant value, there  are other  efficient methods,  for example,  s+zeros(m,n).

Don’t  go overboard.  Inlining  functions  is only beneficial when the  function  is simple  and  when it  is called frequently. Doing it unnecessarily  obfuscates  the code.

Download Full Pdf Version

VN:F [1.8.8_1072]
Rating: 9.0/10 (1 vote cast)
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
Writing Fast Matlab Code #6:Inlining Simple Functions9.0101

Popularity: -0% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Live
  • PDF
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Add to favorites
  • email
  • MySpace
  • RSS

Related Posts

Comments

Comments are closed.