Should table Indexing be Faster?
11 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Example code.
t = combinations(0:10,0:10,0:10,0:10);
tic
for ii = 1:10
for jj = 1:height(t)
u = t{jj,:};
end
end
toc
tic
tcell = table2cell(t);
for ii = 1:10
for jj = 1:height(tcell)
u = [tcell{jj,:}];
end
end
toc
tic
tarr = table2array(t);
for ii = 1:10
for jj = 1:height(tarr)
u = tarr(jj,:);
end
end
toc
Any ideas why indexing into a table to extract data is so slow?
1 Commento
Walter Roberson
il 15 Set 2024
By the way: we held a discussion of table indexing across rows, about two-ish years ago. I know that I contributed, and I seem to remember that Steven Lord contributed. The thread revived briefly a couple of months ago. Unfortunately I do not seem to be able to locate it at the moment.
Risposta accettata
Matt J
il 16 Set 2024
Modificato: Matt J
il 16 Set 2024
I haven't profiled it, but I would bet that the following line, from @tabular/braceReference
b = t.extractData(varIndices);
is bottlenecking the row extraction operations. Effectively, this runs table2array(t) on the entire table t every time a braceReferencing operation is done.
That probably should be done differently, since as a result, the time for even the smallest row-extraction operation is a very strong function of the size of the table, see example below:
T = combinations(1:100,1:100,1:40,1:40);
t=T(1,:);
timeit(@() t{1,:})
timeit(@() T{1,:})
6 Commenti
Matt J
il 18 Set 2024
Modificato: Matt J
il 18 Set 2024
From Tech Support:
Thank you for identifying a performance slowdown when extracting rows from a table. My colleagues are aware of the issue and are working on a fix. In the meantime, a workaround is to use parenthesis subscripting.
For example, currently you are extracting rows using the following syntax. Instead, extract rows using parentheses.
T = combinations(1:100,1:100,1:40,1:40);
t = T(1,:);
When I compared the elapsed time for both these syntaxes, the suggested workaround of parentheses was significantly faster than the original.
%Original example
tic, for i = 1:100, t1 = t{1,:}; end, toc
Elapsed time is 0.005140 seconds.
tic, for i = 1:100, t1 = T{1,:}; end; toc
Elapsed time is 4.458811 seconds.
% Workaround
tic, for i = 1:100, t1 = t(1,:); t1 = t1.Variables; end, toc
Elapsed time is 0.006460 seconds.
tic, for i = 1:100, t1 = T(1,:); t1 = t1.Variables; end; toc
Elapsed time is 0.005319 seconds.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!