Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to perform function inlining in MATLAB?

What language feature or outside-the-box hack can I use to accomplish function inlining in MATLAB? Annoyingly, a Google search for "matlab inline function" reveals that MATLAB's designers thought that "to inline" means "to construct an anonymous function out of a string" (...wtf?).

The reason I ask is I am writing a script that needs to run fast, and I am encountering a lot of situations where it would be nice to have a helper function handle some simple addition or something to avoid off-by-one errors. However, the cost of a function call (as measured by tic/toc) would not be worth it. I refuse to believe that MATLAB would lack a function inlining feature because that would discourage decomposition!

Worst case, I could resort to using M4 macros.


EDIT: In response to eat's comment, here is a situation where I might like to inline a helper:

At one point, I need to loop (yeah, I know) over all windows of width windowWidth within a list:

lastWindowStartIdx = length(list) - windowWidth + 1;
for windowStartIdx = 1:lastWindowStartIdx
   display(list[windowStartIdx:windowStartIdx+windowWidth-1]); %the window we're looking at
end

It would be nice to be able to factor out those tricky, off-by-one-error-prone windowWidth calculations. E.g.:

lastWindowStartIdx = calcLastWindowStartIdx(length(list), windowWidth);
for windowStartIdx = 1:lastWindowStartIdx
   display(list[windowStartIdx:calcWindowEndIdx(windowStartIdx, windowWidth)]); %the window we're looking at
end
like image 527
AlcubierreDrive Avatar asked Jan 21 '23 06:01

AlcubierreDrive


1 Answers

Specific answer:

In reference to your example use-case, this honestly strikes me as a sort of micro-optimization. These plus-one operations are certainly not the bottleneck in your code keeping it from running fast, so you should instead focus on speeding up the parts of your code where the bulk of your computations take place.

If you are only wanting to make this change for aesthetic (spelled it on the first try!) reasons, or because you just don't like keeping track of the extra ones, then creating a function to handle it as in your example is a bad choice in my opinion because it just makes the code that much harder to read and understand. I would suggest simply creating a new variable like so:

windowOffset = windowWidth - 1;
lastWindowStartIdx = length(list) - windowOffset;
for windowStartIdx = 1:lastWindowStartIdx
   display(list[windowStartIdx:windowStartIdx + windowOffset]);
end

General answer:

Regarding your more general question about how to create true inline functions in MATLAB like you can in C or C++, I don't believe there's any way to do this. I thought perhaps that an anonymous function may fit the bill, but after doing some simple timing comparisons with a number of different types of functions in MATLAB I found that an anonymous function is actually slower than a good ol' subfunction for a simple +1 operation.

Here were the sample functions I tested:

function parent_function
  add_one_anon = @(A) A+1;         %# An anonymous function
  add_one_inline = inline('A+1');  %# An "inline" function
  function add_one_nest            %# A nested function
    A = A+1;
  end
  %# Did the timing here ...
end
function A = add_one_sub(A)        %# A subfunction
  A = A+1;
end

I ran each of these 100,000 times incrementing a scalar value that started at 1, and here are the results:

            | Time (sec)
------------+------------       
subfunction |   0.0507
anonymous   |   0.0672
nested      |   0.0932
inline      |  14.7095

If anonymous functions acted like true inline functions, I would have expected them to be the fastest solution.

like image 112
gnovice Avatar answered Jan 31 '23 07:01

gnovice