我的组织通常比最新的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