MATLAB Answers

Can I somehow improve performance of str2double?

76 views (last 30 days)
MichaU709
MichaU709 on 22 Jan 2021
Commented: Paul on 10 Feb 2021 at 1:53
Hello, Is there some solution to improve perfomance by using some other method than str2double while I am converting huge arrays of strings? My whole function needs almost 15 seconds to convert these strings. It affects significantly to perfomance of my script.
Elapsed time is 7.239212 seconds.
Elapsed time is 6.984212 seconds.

  2 Comments

Steven Lord
Steven Lord on 22 Jan 2021
What do you mean by "huge" in "huge array of strings"? Are we talking on the order of 1,000 strings; 1,000,000 strings; 1,000,000,000 strings; etc.?
If it takes MATLAB 15 seconds to convert 15 thousand strings, that's quite a different situation than MATLAB taking 15 seconds to convert 15 million strings.
MichaU709
MichaU709 on 22 Jan 2021
Sorry for not specifying.I am trying to converter about 1 000 000 strings.

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 22 Jan 2021
Edited: Stephen Cobeldick on 22 Jan 2021
The fastest conversion uses low-level commands, e.g. sprintf and sscanf. Instead of this:
C = {'1.2','3.4','5.6'};
V = str2double(C)
V = 1×3
1.2000 3.4000 5.6000
do this (add reshape if required, or specify the optional size argument):
V = sscanf(sprintf(' %s',C{:}),'%f',[1,Inf])
V = 1×3
1.2000 3.4000 5.6000
See also:

  1 Comment

Sign in to comment.

More Answers (1)

Yair Altman
Yair Altman on 9 Feb 2021 at 22:51
I use the following function to convert a cell-array of strings to a cell-array of numeric values (where applicable) - it is ~2-5x faster than str2double or sscanf, depending on the specific inputs (YMMV). It is fastest for positive integers up to 10^15-1, still fast for doubles, and about the same speed as sscanf for all other inputs. Note that strings are left unchanged in the input cell-array.
function results = strs2number(cellStrs)
numVals = numel(cellStrs);
results = cellStrs; % pre-allocate
powersOf10 = [10000000000000 1000000000000 100000000000 10000000000 ...
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1];
Nmax = 14; %=length(powersOf10);
%results(cellfun('isempty',results)) = {[]}; % it's faster to loop below
for idx = 1 : numVals
value = cellStrs{idx};
N = length(value);
if N==0
results{idx} = []; % '' => []
continue
elseif value(1) > '9' % skip the most obvious non-numeric strings first
continue
elseif N > Nmax % e.g. '20210209 08:35:40'
continue
end
%results{idx} = str2number(value); % inlined below for performance
isDigit = value>='0' & value<='9';
if all(isDigit) %simple positive integer
powers = powersOf10(Nmax-N+1:Nmax); %faster than powersOf10(end-N+1:end) or 10.^(N-1:-1:0)
results{idx} = sum((value-'0') .* powers);
else
isDot = value=='.';
if all(isDigit | isDot) % && N <= Nmax %simple positive FP number
dotIdx = find(isDot);
if numel(dotIdx) > 1, continue, end % ignore IP addresses: '12.34.56.78'
N = N - 1;
shift = dotIdx - N - 1;
factor = 10^shift;
n1 = Nmax-N; n2 = n1+dotIdx;
powers = [powersOf10(n1+1:n2-1), 0, powersOf10(n2:Nmax)];
numericVal = sum((value-'0') .* powers) * factor;
results{idx} = numericVal;
elseif any(value==' ') || any(value=='/') || any(value>'9') || sum(value=='-') > 1 || sum(isDot) > 1 %||value(1)>'9' % non-numeric string (we assume that FP values like '1.23e-5' will never happen)
%results{idx} = value; % unnecessary: already pre-allocated this way
else % negative number etc.
[numericVal,count,errMsg,nextIndex] = sscanf(value,'%f',1); %*much* faster than str2double!
if count == 1 && isempty(errMsg) && nextIndex > N
results{idx} = numericVal;
else
%results{idx} = value; % unnecessary: results was initialized to cellStrs
end
end
end
end
end

  1 Comment

Paul
Paul on 10 Feb 2021 at 1:53
I've had the same question as the OP, but using a string array instead of cell array. Seems to me that for string arrays with "basic" numbers, jsut casting to double is much, much faster than str2double.
>> s = string(randn(1000000,1));
>> c = cellstr(s);
>> tic,str2double(s);toc
Elapsed time is 14.454159 seconds.
>> tic,double(s);toc
Elapsed time is 0.084145 seconds.
>> tic,strs2number(c);toc
Elapsed time is 8.621488 seconds.
Is there any catch to using double insteand of str2double?

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!