필터 지우기
필터 지우기

Differences in memory pre-allocation approaches.

조회 수: 2 (최근 30일)
Reynaldo
Reynaldo 2013년 5월 27일
답변: Jan 2017년 1월 18일
Why should we prefer one pre-allocation approach over the other?
I have compiled three different approaches that apparently pre-allocates memory in MATLAB.
clear all
clc
N = 2^28;
tStart = tic;
% Approach #1:
%X = zeros(N,1);
%Approach #2:
% X = [];
% X(N) = 0;
%Approach #3:
%X = [];
%X(N, 1) = 0;
%TEST CODE
%Code that uses the previous pre-allocation approaches.
%for i = 1:N
%
% X(i) = i;
%
% end
tStop = toc(tStart)
Observations (All approaches equally speed up the test code.):
Approach #1: When I use this pre-allocation approach, MATLAB effectively reserves the memory, and this memory usage shows up on the Task Manager. The task manager shows MATLAB is using 2.50 GB before and after executing the test code.
Approach #2: This approach has the same behavior as approach #1.
Approach #3: When I use this pre-allocation approach, before executing the test code, the Task Manager shows MATLAB is using only about 500MB of memory, but after executing the test code it uses 2.50 GB, as seen on approach #1 and #2.
Does it mean that Approach #3 is more memory efficient than the other two approaches? It seems as if Approach #3 is pre-allocating memory, but it is not using it until required. Thanks!
  댓글 수: 3
James Tursa
James Tursa 2013년 5월 27일
I'm curious ... what behavior do you get if you use this mex routine for the pre-allocation?
Cedric
Cedric 2013년 5월 27일
I'll have to make a few tests about #3, but Yair discusses #1 and #2 in http://undocumentedmatlab.com/blog/preallocation-performance/ (and associated threads).

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

답변 (2개)

per isakson
per isakson 2013년 5월 27일
This has been discussed more than once. I think Loren has discussed it in her blog.
Some comments
  • The behavior of Matlab may change from one release to the next. Thus, always state which release.
  • With R2012a #1 allocates memory and assigns the value 0 to all elements. #2 and #3 takes a "lazy" approach. Matlab "reserves" memory internally and only the last element is assigned a value. The memory is not used until values are assign by your for-loop.
  • Try tic, ..., toc the three approaches and I think you will see that #2 and #3 are much faster the #1.
  댓글 수: 1
Greg
Greg 2017년 1월 18일
I know this is a really old thread, but I'm not comfortable with the implication of this answer. Methods #2 and #3 probably are faster at the pre-allocation, but the subsequent for loop will likely be much slower because MATLAB hasn't fully allocated that memory. This holds with my experience (sorry, I don't have any concrete examples to offer). I always suggest using zeros/ones/nan/inf/true/false functions to preallocate arrays if for no other reason than it's clear and obvious.

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


Jan
Jan 2017년 1월 18일
For real world applications the actual work is the same: Either Matlab can reserve the memory lazily at first or allocate zeroed memory directly. At least since the data are written to the array, the explicit allocation must have happend. Therefore measuring the run time should include the process of writing.

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by