Why does SORT outperform SORTROWS?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Any theories as to why SORT + row re-ordering outperforms SORTROWS when sorting with respect to a single column:
x=rand(18e6,7);
tic;
[~,idx]=sort(x(:,1));
y1=x(idx,:);
toc
%Elapsed time is 3.589928 seconds.
tic;
y2=sortrows(x,1);
toc
%Elapsed time is 14.869106 seconds.
0 Kommentare
Akzeptierte Antwort
Jan
am 30 Dez. 2012
Bearbeitet: Jan
am 30 Dez. 2012
The profile shows, that the additional time is spent in the C-Mex function sortrowsc.mexw64. At least in R2009a the source code has been shipped with Matlab. Inside the C-Mex, the not stable qsort is called, which is obviously slower than Matlab's sort algorithm.
Copy sortrows.m and change the line 74 of from:
ndx = sortrowsc(x_sub, col);
to:
ndx = sort_back_to_front(x_sub, col);
to let the main work be done by sort. For a productive use, cells must be considered also, see some lines later for the general case in the else branch. It is ironic, that this is the "old Matlab 6.0" fallback, which has been obviously much faster.
Please send an enhancement request to TMW.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!