Why does str2num take much longer to convert 1001 long char array apposed to a 1000 long char array (MatLab R2010b 64bit, on Win 64bit)

9 views (last 30 days)
Serge on 1 Jun 2016
Commented: Walter Roberson on 2 Jun 2016
Example:
%make 1001-by-3 char array of numbers, can replace 3 with any size
t = '0123456789';
t = t(ceil(rand(3,1001)*10))';
%big difference in time needed to convert 1000 vs 1001 numbers
tic,n1000 = str2num(t(1:1000,:));toc
tic,n1001 = str2num(t(1:1001,:));toc
% Elapsed time is 0.000285 seconds.
% Elapsed time is 0.011493 seconds.
%results are identical
all(n1000 == n1001(1:1000))
%ans =
% 1

Walter Roberson on 1 Jun 2016
You should use timeit() from the File Exchange to get more accurate timing. (timeit was eventually made part of MATLAB.)
I do see more of a difference than I would expect in R2016a on OS-X.
I recommend you switch to str2double()

Serge on 2 Jun 2016
str2double is much slower then str2num for a char array and speed is an important aspect of this question.
t1001 = num2str(rand(1001,1)); t1000 = t1001(1:1000,:);
c1001 = cellstr(t1001); c1000 = cellstr(t1000);
f = @()str2num(t1000); t1 = timeit(f)
f = @()str2num(t1001); t2 = timeit(f)
f = @()str2double(cellstr(t1000)); t3 = timeit(f)
f = @()str2double(cellstr(t1001)); t4 = timeit(f)
f = @()str2double(c1000); t5 = timeit(f)
f = @()str2double(c1001); t6 = timeit(f)
t1 = 0.00054 %FASTEST!
t2 = 0.0119 %
t3 = 0.0208
t4 = 0.0213
t5 = 0.0190
t6 = 0.0187
This means that the fastest way to read a regularly formatted file is to read it in chunks, convert text to an array, then use str2num, but making sure that the initial chunk does not read in more then 1000 lines. Even faster then fscanf.
Walter Roberson on 2 Jun 2016
f = @() str2double(t1000); t7 = timeit(f)
f = @() str2double(t1001); t8 = timeit(f)
On my system it is not as fast as t1 or t2 but it is faster than any of the others.