Can I somehow improve performance of str2double?
42 次查看(过去 30 天)
显示 更早的评论
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 个评论
Steven Lord
2021-1-22
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.
采纳的回答
更多回答(1 个)
Yair Altman
2021-2-9
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
3 个评论
Florian Berzsenyi
2023-1-3
I assume that str2double is simply the more robust method at the (high) cost of processing time. str2double checks the type and parses for separators (.), or has at least has a larger overhead to account for the conversion from scientific-, double- or plain notation to double or integer numbers. double, is a simple casting function. It may work, if your string array absolutely always cotains digits 0-9, or it may fail if you try to convert uncleaned data with "nans", text "nans".
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!