필터 지우기
필터 지우기

Efficient use of memory

조회 수: 1 (최근 30일)
emar
emar 2017년 6월 8일
댓글: Philip Borghesani 2017년 6월 8일
Hello,
If i create a cell with 1000 matrices ( size of each matrix 800*1280), clearing each matrix after using it will speed up calculations ?
Example
A=cell(1000,1);
for i=1:1000
A{i}=rand(800,1280);
end
image=A{1};
image2=A{2}; % I will use image and image2 with other functions
A{1}=[];
A{2}=[];
EDIT
The real use of the cell will be like :
A=cell(1000,1);
parfor i=1:1000
A{i}=function_that_creates_image(800,1280); % image with size 800*1280 px
end
for i=1:number_of_images % number_of_images=1000 in this case
image1=A{1};
image2=A{2};
A{1}=[];
A{2}=[];
% image1 and image 2 will be used then in the next lines
%next lines of code
end
I noticed that calculating components of A in a parfor loop is faster than calculating each component for each loop inside the for loop
Thank you in advance
  댓글 수: 2
Adam
Adam 2017년 6월 8일
편집: Adam 2017년 6월 8일
Firstly you would be better using a 3d numeric array. Or rather, firstly, why do you want 1000 large arrays all at once? Your code only shows you using 2 of them so it's impossible to make meaningful suggestions on how efficient it is having the other 998 in memory. As for clearing the 2 you have used, you should be able to see if the memory goes down in your task manager. I don't know if the memory gets free'd instantly.
As you have shown it though there is no reason to store any more than one matrix at a time.
emar
emar 2017년 6월 8일
I edited the question for more explanations

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2017년 6월 8일
clearing each matrix after using it will speed up calculations ?
It depends on the available RAM. If the RAM is exhausted and the much slower virtaul RAM is used instead, the speed difference can be factor 100 or higher. As soon as memory is released, the chance gorws, that all data match into the RAM.
Note that
image1=A{1};
creates a "shared data copy", such that only about 100 Bytes are used in addition, while the actual data are shared. Then:
A{1}=[];
does not release the memory directly, but this happens after image1 is overwritten or cleared also.
As Adam, I would expect that creating only the data required for the current iteration is faster. This would avoid to store 998 images in the memory without need.
  댓글 수: 1
Philip Borghesani
Philip Borghesani 2017년 6월 8일
In addition if image1 or image2 is modified using indexing after clearing A{1} and A{2} no copy on write is needed because there is no remaining sharing of the data. The in place optimization will also be enabled for image1 and 2.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by