The solutions below are ordered roughly from fastest to slowest. Note how the solutions fall into three order-of-magnitude performance classes.
This is using precompiled Octave 4.2.2 from MacPorts on an iMac; octave @4.2.2_1+accelerate+app+docs+fltk+gfortran+graphicsmagick+qt5+sound
.
Elapsed time is 0.00452113 seconds.
Elapsed time is 0.0121579 seconds.
Elapsed time is 0.0185781 seconds.
Elapsed time is 0.0243361 seconds.
Elapsed time is 0.025944 seconds.
Elapsed time is 2.42572 seconds.
Elapsed time is 2.4809 seconds.
Elapsed time is 2.48733 seconds.
Elapsed time is 2.49299 seconds.
Takeaways: Prefer sprintf
over any other to-string conversions, and ostrsplit
over strsplit
.
clear all
A=rand(1,2000);
#A=1:2000;
tic
A4=ostrsplit(sprintf("%g ",A), " ", true);
toc;tic
A9=ostrsplit(num2str(A), " ", true);
toc;tic
A8=regexp(num2str(A), '\s+', 'split');
toc;tic
A3S=num2str(A');
A3=mat2cell(A3S,ones(1,size(A3S,1)))';
A3=strtrim(A3);
toc;tic
A5=strsplit(num2str(A));
toc;tic
A7=cellfun(@num2str, num2cell(A), 'uni', 0);
toc;tic
A6=arrayfun(@(x) {num2str(x)}, A);
toc;tic
A2=cell(size(A));
for i = 1:numel(A)
A2{i} = num2str(A(i));
endfor
toc;tic
for i = 1:numel(A)
A1{i} = num2str(A(i));
endfor
toc;tic