speed, functions and overheads

Dear all,
I have noticed using functions, nested functions and sub-functions considerably decrease the speed of execution. Is it possible to decrease the overhead incurred when calling a function?
Thanks, Patrick

2 comentarios

Matt J
Matt J el 24 de Jun. de 2013
Decrease it from what? What baseline are you comparing it to?
Sean de Wolski
Sean de Wolski el 24 de Jun. de 2013
What are you doing?
Have you used the profiler to identify bottlenecks in your code that are more than likely killing way more time than function overhead?

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 24 de Jun. de 2013
Editada: Jan el 25 de Jun. de 2013

0 votos

x = rand(1000, 1000);
tic;
for k = 1:1e3
m = mean(x);
end
toc;
tic;
for k = 1:1e3
m = sum(x, 1) / size(x, 1);
end
toc;
I assume, but cannot test currently, that there is a measurable difference in the timing due to the overhead for calling the M-function mean() (at least until 2009a it was an M-file).
Of course there is no magic flag to reduce the overhead, otherwise TMW would not have disabled this. So inlining the code is the only way to avoid the overhead. The resulting code is faster for the price of a reduced readability.
[EDITED] Summary of my comments:
  • When you call functions, you cannot avoid the overhead.
  • When you inline the code instead, the overhead vanishes, but other difficulties arise.
  • Tertium non datur.

7 comentarios

Sean de Wolski
Sean de Wolski el 24 de Jun. de 2013
Saved about 60ms.
Patrick Mboma
Patrick Mboma el 24 de Jun. de 2013
Hi Jan, Your example is a rather simple one. More concretely, try and compare mean and this simple function
function m=mymean(x)
m=mean(x);
end
The overhead is just enormous. Unfortunately not all functions can be written as inline functions. Anonymous functions do not remove the overhead either.
Matt J
Matt J el 24 de Jun. de 2013
Unfortunately not all functions can be written as inline functions.
You have to find a way. Or you have to find a way to do more vectorization. Or, you have to use PARFOR, if you have the parallel computing toolbox.
It has always been MATLAB programming philosophy that you avoid lots of mfile function calls in large loops.
Jan
Jan el 24 de Jun. de 2013
Editada: Jan el 24 de Jun. de 2013
@Patrick: Yes, I've chosen the most simple example I could find. You see both effects already:
  1. In R2009a the inlined (not "inline"!) function is 10% faster than calling mean().
  2. The mean() version looks cleaner. And if the example is e.g. expanded to nanmean() the inlined version looks cruel already.
Using small functions for special job is easier to maintain, debug and expand as well as it allows re-using established code. Inlining decreases the runtime by avoiding the overhead of calling a function and creating (shared) data copies. So as usual the programmer has to take into account the different processes which are required to solve a problem:
solution = design + program + validate + debug + runtime
So optimizing the runtime by some seconds does not allow to solve the problem faster, when it leads to hours of debug time. Therefore only the bottlenecks should be inlined into the loops, see e.g. Answers: condition-the-number-of-nested-for-loops: here SUB2IND had a lot of security checks and too much generality to handle the simple 2D case efficiently.
Patrick Mboma
Patrick Mboma el 25 de Jun. de 2013
@Jan, The examples are definitely too simple. If I could just inline a function with fifty of lines of code and return different types of output...
Jan
Jan el 25 de Jun. de 2013
Editada: Jan el 25 de Jun. de 2013
@Patrick: Let me repeat again, that the example is not "too simple" but chosen as simple as possible on purpose to demonstrate the drawbacks and benefits of inlining versus calling functions.
Of course I know, that calling mean() will not solve your problem. But you ask for avoiding the overhead of calling a function. And the answer is simply: See [EDITED].
TallBrian
TallBrian el 15 de Oct. de 2019
@Jan, this answer was from back in 2013, do you know is the answer still the same with the latest MATLAB?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Function Creation en Centro de ayuda y File Exchange.

Preguntada:

el 24 de Jun. de 2013

Comentada:

el 15 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by