Memory-efficient anonymous functions

버전 1.1.0.0 (2.44 KB) 작성자: Matt J
Obtain anonymous function clean of non-essential workspace variables
다운로드 수: 381
업데이트 날짜: 2015/11/6

라이선스 보기

It may or may not be widely known, but as of the time of this submission (R2015a), anonymous function handles can store large, unused chunks of memory from the workspace where they are created. Often, anonymous functions are used transiently and the effect of this is never felt. If, however, you return an anonymous function handle as the output of a function, or save it to a .mat file, or broadcast it to workers using the Parallel Toolbox's PARFOR command, you may see lots of overhead from this hidden data. For examples and further discussion, see these threads
http://www.mathworks.com/matlabcentral/answers/115569-what-extra-data-is-stored-by-an-anonymous-function

http://www.mathworks.com/matlabcentral/answers/115254-parfor-behavior-sensitive-to-comments

This submission contains the function AFSLIM, which will let you create an anonymous function that stores only the extra parameter variables that you specify. (Naturally, of course, you must specify all the variables that it needs.)

EXAMPLE: The following code (important - must be run as a function, not from the command line!!) generates two files containing an anonymous function with the same functional behavior. However, tst1.mat consumes 259 MB whereas tst2.mat consumes only 1 KB.

function test

b=2;

fun1=@(x)x+b; %anonymous function with parameter 'b'

fun2=afslim(fun1,b); %slim version

b=rand(6000);

save tst1 fun1
save tst2 fun2

end

If you are given an existing anonymous function which already contains lots of unwanted extra data, this submission also contains AFCLEAN, which tries to strip the extra data away. However, AFCLEAN relies on a certain amount of undocumented MATLAB and breaks anonymous functions that refer to nested or subfunctions. When it works, it seems to work well, but use at your own risk!

인용 양식

Matt J (2024). Memory-efficient anonymous functions (https://www.mathworks.com/matlabcentral/fileexchange/45749-memory-efficient-anonymous-functions), MATLAB Central File Exchange. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R2013b
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!
버전 게시됨 릴리스 정보
1.1.0.0

Added info to Description section.
added afslim.m

1.0.0.0