Call subfunction many times: Nested or local

12 visualizaciones (últimos 30 días)
Kai
Kai el 22 de Oct. de 2018
Comentada: Kai el 22 de Oct. de 2018
I have a question regarding performance. Consider the following code:
test1 = 0;
for k = 1:100000
test1 = testfun(test1);
end
test2 = 0;
for k = 1:100000
test2 = test2+1;
end
which uses the function
function j = testfun(i)
j=i+1;
end
Simply explained, the code does the same calculation twice, the first time using a local subfunction, the second time using a nested subfunction. In the MathWorks documentation there is an article on improving performance, and there it is recommended to use local instead of nested functions (see here).
However, when I run the above code, the first part (local function) needs about 50 times longer than the second part (nested function). So I am wondering if I maybe misunderstood something, or if there are other factors which should be considered when deciding between those two options. I kinda suspect that when a simple process is repeated many times, the nested version is more efficient, because in this case calling the local function needs more time compared to actually executing it.
I have a similar situation with a more complex subfunction which I also call many times. It has about 25 lines, but also mostly consists of basic calculations on arrays with about 1-5 entries. So far I have written it down as a nested subfunction and I was wondering if I could improve the performance by instead writing a local function, but the above test-code doesn't give me much hope.
  1 comentario
Matt J
Matt J el 22 de Oct. de 2018
Editada: Matt J el 22 de Oct. de 2018
In the second version, I don't see any function calls at all, nested or otherwise.

Iniciar sesión para comentar.

Respuesta aceptada

OCDER
OCDER el 22 de Oct. de 2018
Editada: OCDER el 22 de Oct. de 2018
In-line codes are normally fastest, but you should use local function if:
  • it is going to be used ONLY by the main function
  • the main function uses it multiple times in different locations
  • you need to create temporary arrays and do not want to clear them (which is slow)
  • you want to be organized for future code edits
I normally start with in-line code, then move to local function, and if another function requires the same local function, then just save the local function as it's own function. It depends on how reusable you want your codes to be. You don't want to track down every instance of a local function if you want to edit your code. Last, try to avoid nested functions if you can - it's slow and also hard to edit.
function compareSpeed
%------------------------------------------------------------------------------
%LOCAL FUNCTION. OKAY. Good for modular programming.
tic
test1 = 0;
for k = 1:100000
test1 = testfun(test1);
end
toc
%------------------------------------------------------------------------------
%IN-LINE CODE. FAST.
tic
test2 = 0;
for k = 1:100000
test2 = test2 + 1;
end
toc
%------------------------------------------------------------------------------
%NESTED FUNCTION. SLOW.
tic
test3 = 0;
for k = 1:10000
testfun3;
end
toc
function testfun3 %This is a nested function, a function within a function.
test3 = test3 + 1;
end
end %END OF compareSpeed main function
function j = testfun(i) %This is a local function. It is NOT inside the
%function-end statement of the main function.
j = i + 1;
end

Más respuestas (1)

Guillaume
Guillaume el 22 de Oct. de 2018
As Matt said, there's no function call at all in your 2nd example. Function calls (nested, local, or otherwise) are expensive so not having any call is usually going to result in faster code (at the expense of readability and maintenance if the code becomes messy, do use functions!)
Adapting your example to local vs nested function, it would be something like this:
%testlocal.m
function test1 = testlocal
for k = 1:100000
test1 = testfun(test1);
end
end
%local function:
function j = testfun(i)
j = j + 1;
end
%testnested.m
function test2 = testnested
for k = 1:100000
test2 = testfun(test2);
end
%nested function (defined inside another function)
function j = testfun(i)
j = j + 1;
end
end
I would suspect that there is no significant difference betweent the execution time of the two. The main difference between nested functions and local functions is that nested functions have access to the variables in scope of the parent function, a feature that is not used in the above examples.
  1 comentario
Kai
Kai el 22 de Oct. de 2018
Ah okay, thank you. Then I simply misunderstood the meaning of "nested function". So I agree, the code would be more readable with using local functions, but since runtime is really an issue I will try to avoid it and instead try to not have any call. But I assume that if I call the function only a couple of times, it is not much more costly to use a local function, so in that case I would prefer a local function, right?

Iniciar sesión para comentar.

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by