Which type of function call provides better performance in MATLAB?
조회 수: 65 (최근 30일)
이전 댓글 표시
I have 7 different types of function call:
1. An inlined function, where the code author replaces the function call with a copy of the body of the function.
2. A function is defined in a separate MATLAB file. The arguments are passed by the calling function (file-pass).
3. A function is defined in a separate MATLAB file. The arguments are provided by referencing global variables; only indices are provided by the calling function (file-global).
4. A nested function. The arguments are passed by the enclosing function (nest-pass).
5. A nested function. The arguments are those shared with the enclosing function; only indices are provided by the enclosing function (nest-share).
6. A sub function. The arguments are passed by the calling function (sub-pass).
7. A sub function. The arguments are provided by referencing global variables; only indices are provided by the calling function (sub-global).
(For more information, please see the following three MATLAB files: testTop.m, testCompute, and testComputeGlobal.m)
I would like to know which function call provides better performance than the others in general.
채택된 답변
MathWorks Support Team
2018년 10월 5일
편집: MathWorks Support Team
2018년 10월 5일
The ordering of performance of each function call from the fastest to the slowest tends to be as follows:
inlined > file-pass = nest-pass = sub-pass > nest-share > sub-global > file-global
(A>B means A is faster than B and A=B means A is as fast as B)
First, using an inlined function is the fastest as it does not incur overhead associated with function call.
Second, when the arguments are passed to the callee function, the calling function sets up the arguments in such a way that the callee function knows where to retrieve them. This setup associated with function call in general incurs performance overhead, and therefore file-pass, nest-pass, and sub-pass are slower than inline.
Third, if the workspace is shared with nested functions and the arguments to a nested function are those shared within the workspace, rather than pass-by-value, then performance of that function call is inhibited. If MATLAB sees a shared variable within the shared workspace, it searches the workspace for the variable. On the other hand, if the arguments are passed by the calling function, then MATLAB does not have to search for them. The time taken for this search explains that type nest-share is slower than file-pass, nest-pass, and sub-pass.
Finally, when a function call involves global variables, performance is even more inhibited. This is because to look for global variables, MATLAB has to expand its search space to the outside of the current workspace. Furthermore, the reason a function call involving global variables appears a lot slower than the others is that MATLAB Accelerator does not optimize such a function call. When MATLAB Accelerator is turned off with the following command,
feature accel off
the difference in performance between inline and file-global becomes less significant.
Please note that the behaviors depend largely on various factors such as operating systems, CPU architectures, MATLAB Interpreter, and what the MATLAB code is doing.
댓글 수: 1
Walter Roberson
2018년 7월 5일
Also note that anonymous function calls are slower than direct function calls, but the difference can vary from almost negligible to being rather substantial in nearly identical code.
(I have some test cases that I put together a couple of years ago, but my system is loaded at the moment so I cannot do timing tests right now.)
추가 답변 (2개)
Robert
2018년 7월 3일
Helpful stuff, but shouldn't the first alternative read "1. An Inline function. The body of the function is directly written down (inline)."?
댓글 수: 0
broken_arrow
2021년 3월 20일
That leaves me a bit confused. Isn't an inlined function the same as just pasting the function code into a script (which would mean a script should be the most performant)? This post on the other hand suggests that functions are generally faster than scripts: https://de.mathworks.com/matlabcentral/answers/415728-details-on-why-functions-are-faster-than-scripts But if I pass variables to a function, the program would still have to look up the variables in the base workspace and launch the function, which creates overhead compared to a script. My "working hypothesis" used to be that everything that is executed only once is put in a script and functions are for code that is used often...
댓글 수: 23
Walter Roberson
2021년 3월 23일
Interesting, at https://www.mathworks.com/content/dam/mathworks/mathworks-dot-com/images/events/matlabexpo/kr/2016/matlab-programming-techniques-for-efficiency-performance.pdf I find the description
MATLAB Execution Engine
Old system had two different execution mechanisms –a JIT and an Interpreter. New system has a single execution mechanism.
Old JIT was designed for FORTRAN-like constructs within MATLAB. New JIT is designed for the entire MATLAB language.
Old system had a monolithic architecture that was difficult to extend. New system has a Modular, Thread-safe, and Platform re-targetable architecture.
Bruno Luong
2021년 3월 24일
My own reading of such description is that
- old JIT is separated to EE and consisiting of calling prebuilt library to replace MATLAB code,
- new JIT is integrated into EE and is close to a compilation to native machine instructions.
참고 항목
카테고리
Help Center 및 File Exchange에서 MATLAB Compiler에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!