1

我的组织通常比最新的 Matlab 版本晚几年。我发现splitapply当有很多组(两个数字分组变量)时,这非常慢,这与我使用 SQL 的经验形成鲜明对比。我怀疑它会遍历所有组。不管是什么原因,出于长期规划的目的,我想知道是否有人可以评论这是否是 2015b 后 Matlab 版本中的问题?

这是一些基准测试代码。结果并不像我的真正问题那样极端,但它仍然显示了执行时间的差异。

clear all

% This data is more like mine and takes splitapply 3+ minutes to run
%-------------------------------------------------------------------
% tDat = array2table( ...
%     floor([ rand(2e6,1) 20e3*rand(2e6,1) 50*rand(2e6,1) ]) , ...
%     'VariableNames' , {'data2add','groupVar1','groupVar2'} );

% This data runs way faster than mine, but still illustrates the problem
%------------------------------------------------------------------------
tDat = array2table( floor(100*rand(2e6,3)) , ...
                    'VariableNames' , ...
                    {'data2add','groupVar1','groupVar2'} );

[ G , tRollup ] = findgroups( tDat(:,{'groupVar1','groupVar2'}) );

tic
tRollup.total_sa = splitapply( @sum, tDat.data2add, G );
disp('Done splitapply')
toc
fprintf('\n')

tic
tRollup.total_aa1 = accumarray( G, tDat.data2add );
disp('Done accumarray #1')
toc
fprintf('\n')

tic
tRollup.total_aa2 = accumarray( G, tDat.data2add, [], @sum );
disp('Done accumarray #2')
toc
fprintf('\n')

% Confirm that results are equivalent

if isequal( tRollup.total_sa , tRollup.total_aa1 )
   disp('tRollup.total_sa == tRollup.total_aa1')
else
   disp('tRollup.total_sa ~= tRollup.total_aa1')
end

if isequal( tRollup.total_aa1 , tRollup.total_aa2 )
   disp('tRollup.total_aa1 == tRollup.total_aa2')
else
   disp('tRollup.total_aa1 ~= tRollup.total_aa2')
end

输出是:

Done splitapply
Elapsed time is 2.550241 seconds.

Done accumarray #1
Elapsed time is 0.021673 seconds.

Done accumarray #2
Elapsed time is 0.020397 seconds.

tRollup.total_sa == tRollup.total_aa1
tRollup.total_aa1 == tRollup.total_aa2
4

0 回答 0