Preallocating table makes it slower?

조회 수: 12 (최근 30일)
Christian Tieber
Christian Tieber 2020년 5월 5일
댓글: Star Strider 2020년 5월 5일
I have to add data to a table with a loop. I can estimate how large the table approx will be at the end. But i dont know the exact size. I also dont know the exact size of the data added with every iteration. I notized that the needed time grows nearly exponetialy when using vertcat. So i tried to preallocate the table. It made things worse!
Can some one help me out?
thanks in advance
Christian
Wrote some code to demostrate the problem:
rows = 1e6;
columns = 6;
aproxRows = rows * 1.5;
Table= table();
preallTable = array2table(zeros(aproxRows, columns));
preallTime = [];
preallTime(1) = 0;
Time = [];
Time(1) = 0;
start = 1;
for i = 1:1e3
i
addTable = array2table(rand(1000, columns)); %size of table is unknown in reality for every loop pass
End = start-1 + height(addTable);
preallTic = tic;
preallTable(start:End, :) = addTable;
preallTime(end+1) = preallTime(end) + toc(preallTic);
start = End+1;
Tic = tic;
Table = vertcat(Table,addTable);
Time(end+1) = Time(end) + toc(Tic);
end
%cut off unused space
preallTable(start:end, :) = [];
%plot Time
figure
hold on
plot(preallTime)
plot(Time)
legend({'preallTime', 'Time'})

채택된 답변

Star Strider
Star Strider 2020년 5월 5일
I would preallocate the array, then do the array2table call at the end, after the array was created and is stable (nothing more to be added to it).
  댓글 수: 2
Christian Tieber
Christian Tieber 2020년 5월 5일
The data to add, comes as a table. Not shure if a can change that.
Star Strider
Star Strider 2020년 5월 5일
Youi can easily change it to an array using the table2array function. If the variable names in the table are important, you can extract them with:
VarNames = T.Properties.VariableNames;
where ‘T’ is the original table name. When you have finished changing the array, you can the re-define the variable names in the new table (created with array2table) using ‘VarNames’ as the cell array value for the 'VariableNames' name-value pair (providing that the number of the variable names has not changed from the original table).

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by