MATLAB Answers

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)

4 ビュー (過去 30 日間)
Serge
Serge 2016 年 6 月 1 日
コメント済み: Walter Roberson 2016 年 6 月 2 日
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

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 6 月 1 日
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()
  2 件のコメント
Walter Roberson
Walter Roberson 2016 年 6 月 2 日
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.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by