更新:我对我之前的“方法 3”不满意,所以我只是重新调整了一下以获得更好的性能。它现在的运行速度几乎是 naive 的 10 倍strcmp
。
strcmp
在我的机器上获胜(Linux Mint 12 上的 2011b)。特别是,它比ismember
. 但是,如果您自己进行一些手动预分类,您可以获得一些额外的速度。考虑以下速度测试:
NumIter = 100;
N = 495000;
K = N / 20;
List = cell(N, 1);
for i = 1:20
List(i*K - K + 1:i*K) = cellstr(char(i+96));
end
StrToFind = cell(NumIter, 1);
for j = 1:NumIter
StrToFind{j} = char(round(rand * 20) + 96);
end
%# METHOD 1 (ismember)
tic
for j = 1:NumIter
Index1 = ismember(List, StrToFind{j});
Soln1 = List(Index1);
end
toc
%#METHOD 2 (strcmp)
tic
for j = 1:NumIter
Index2 = strcmp(StrToFind{j}, List);
Soln2 = List(Index2);
end
toc
%#METHOD 3 (strmp WITH MANUAL PRE-SORTING)
tic
for j = 1:NumIter
CurStrToFind = StrToFind{j};
K = 100;
I1 = zeros(K, 2); I1(1, :) = ones(1, 2);
I2 = zeros(K, 2); I2(end, 1) = 1; I2(end, 2) = N;
KDiv = floor(N/K);
for k = 2:K-1
CurSearchNum = k * KDiv;
CurListItem = List{CurSearchNum};
if CurListItem < CurStrToFind; I1(k, 1) = 1; end;
if CurListItem > CurStrToFind; I2(k, 1) = 1; end;
I1(k, 2) = CurSearchNum; I2(k, 2) = CurSearchNum;
end
a = find(I1(:, 1), 1, 'last');
b = find(I2(:, 1), 1, 'first');
ShortList = List(I1(a, 2):I2(b, 2));
Index3 = strcmp(CurStrToFind, ShortList);
Soln3 = ShortList(Index3);
end
toc
输出是:
Elapsed time is 6.411537 seconds.
Elapsed time is 1.396239 seconds.
Elapsed time is 0.150143 seconds.